[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 | |
---|
| 51 | *inst = self; |
---|
| 52 | return 0; |
---|
[2663] | 53 | } |
---|
| 54 | |
---|
| 55 | int cont_fw_delete (struct cont_fw_parent_inst * inst) { |
---|
[2664] | 56 | int i; |
---|
| 57 | |
---|
| 58 | if ( inst == NULL ) |
---|
| 59 | return -1; |
---|
| 60 | |
---|
| 61 | // check if there are streams to close... |
---|
| 62 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
| 63 | if ( inst->child[i] != NULL ) { |
---|
| 64 | return -1; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
[2670] | 68 | if ( inst->pcb.close != NULL ) |
---|
| 69 | inst->pcb.close(inst); |
---|
| 70 | |
---|
[2664] | 71 | free(inst); |
---|
| 72 | |
---|
| 73 | return 0; |
---|
[2663] | 74 | } |
---|
| 75 | |
---|
[2665] | 76 | int cont_fw_set_uinst(struct cont_fw_parent_inst * inst, void * u_inst) { |
---|
| 77 | if ( inst == NULL ) |
---|
| 78 | return -1; |
---|
| 79 | |
---|
| 80 | inst->u_inst = u_inst; |
---|
| 81 | |
---|
| 82 | return 0; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | int cont_fw_get_uinst(struct cont_fw_parent_inst * inst, void ** u_inst) { |
---|
| 86 | if ( inst == NULL || u_inst == NULL ) |
---|
| 87 | return -1; |
---|
| 88 | |
---|
| 89 | *u_inst = inst->u_inst; |
---|
| 90 | |
---|
| 91 | return 0; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // VIOs: |
---|
[2668] | 95 | int cont_fw_new_child(struct cont_fw_parent_inst * inst, int id) { |
---|
[2672] | 96 | struct cont_fw_child_vio_inst * self; |
---|
[2673] | 97 | struct roar_stream_server * ss; |
---|
[2672] | 98 | int i; |
---|
| 99 | int cid = -1; |
---|
| 100 | |
---|
| 101 | if ( inst == NULL || id == -1 ) |
---|
| 102 | return -1; |
---|
| 103 | |
---|
[2673] | 104 | if ( streams_get(id, &ss) == -1 ) |
---|
| 105 | return -1; |
---|
| 106 | |
---|
[2672] | 107 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
| 108 | if ( inst->child[i] == NULL ) { |
---|
| 109 | cid = i; |
---|
| 110 | break; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if ( cid == -1 ) |
---|
[2668] | 115 | return -1; |
---|
| 116 | |
---|
[2672] | 117 | if ( (self = malloc(sizeof(struct cont_fw_child_vio_inst))) == NULL ) |
---|
| 118 | return -1; |
---|
| 119 | |
---|
| 120 | memset(self, 0, sizeof(struct cont_fw_child_vio_inst)); |
---|
| 121 | |
---|
| 122 | self->parent = inst; |
---|
| 123 | self->child = id; |
---|
| 124 | self->u_inst = NULL; |
---|
| 125 | |
---|
| 126 | inst->child[i] = self; |
---|
| 127 | |
---|
| 128 | if ( inst->pcb.new_child != NULL ) { |
---|
| 129 | if ( inst->pcb.new_child(inst, self) == -1 ) { |
---|
| 130 | inst->child[i] = NULL; |
---|
| 131 | free(self); |
---|
| 132 | return -1; |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
[2673] | 136 | // no error possible here. |
---|
| 137 | cont_fw_init_vio(&(ss->vio), self); |
---|
| 138 | |
---|
| 139 | streams_set_fh(id, -1); // update some internal structures |
---|
| 140 | |
---|
[2672] | 141 | return 0; |
---|
[2668] | 142 | } |
---|
[2665] | 143 | |
---|
[2662] | 144 | int cont_fw_init_vio(struct roar_vio_calls * vio, void * inst) { |
---|
| 145 | if ( vio == NULL ) |
---|
| 146 | return -1; |
---|
| 147 | |
---|
| 148 | memset(vio, 0, sizeof(struct roar_vio_calls)); |
---|
| 149 | vio->inst = inst; |
---|
| 150 | |
---|
| 151 | vio->read = cont_fw_read; |
---|
| 152 | vio->write = cont_fw_write; |
---|
| 153 | vio->sync = cont_fw_sync; |
---|
| 154 | vio->close = cont_fw_close; |
---|
| 155 | |
---|
| 156 | return 0; |
---|
| 157 | } |
---|
| 158 | |
---|
[2661] | 159 | ssize_t cont_fw_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 160 | _BASIC(); |
---|
| 161 | |
---|
[2667] | 162 | if ( inst->parent->ccb.read != NULL ) |
---|
[2669] | 163 | return inst->parent->ccb.read(inst->parent, inst, buf, count); |
---|
[2661] | 164 | |
---|
| 165 | return -1; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | ssize_t cont_fw_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 169 | _BASIC(); |
---|
| 170 | |
---|
[2667] | 171 | if ( inst->parent->ccb.write != NULL ) |
---|
[2669] | 172 | return inst->parent->ccb.write(inst->parent, inst, buf, count); |
---|
[2661] | 173 | |
---|
| 174 | return -1; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | off_t cont_fw_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
| 178 | int cont_fw_nonblock(struct roar_vio_calls * vio, int state); |
---|
| 179 | |
---|
| 180 | int cont_fw_sync (struct roar_vio_calls * vio) { |
---|
| 181 | _BASIC(); |
---|
| 182 | |
---|
[2667] | 183 | if ( inst->parent->ccb.flush != NULL ) |
---|
[2669] | 184 | return inst->parent->ccb.flush(inst->parent, inst); |
---|
[2661] | 185 | |
---|
| 186 | return 0; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | int cont_fw_close (struct roar_vio_calls * vio) { |
---|
| 190 | _DECL(); |
---|
| 191 | int r = 0; |
---|
[2671] | 192 | int i; |
---|
[2661] | 193 | |
---|
| 194 | _PREP(); |
---|
| 195 | |
---|
| 196 | if ( cont_fw_sync(vio) == -1 ) |
---|
| 197 | r = -1; |
---|
| 198 | |
---|
[2667] | 199 | if ( inst->parent->ccb.close != NULL ) |
---|
[2669] | 200 | return inst->parent->ccb.close(inst->parent, inst); |
---|
[2667] | 201 | |
---|
[2671] | 202 | for (i = 0; i < CONT_FW_MAX_CHILDS; i++) { |
---|
| 203 | if ( inst->parent->child[i] == inst ) { |
---|
| 204 | inst->parent->child[i] = NULL; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | free(inst); |
---|
| 209 | |
---|
[2661] | 210 | return r; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | int cont_fw_ctl (struct roar_vio_calls * vio, int cmd, void * data); |
---|
| 214 | |
---|
[2675] | 215 | // CF: |
---|
| 216 | int cont_fw_cf_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
| 217 | struct roar_stream_server * info, |
---|
[2677] | 218 | struct roar_codecfilter * filter) { |
---|
[2678] | 219 | struct cont_fw_parent_inst * self; |
---|
[2683] | 220 | CONT_FW_SETUP_TYPE((*setup)); |
---|
[2678] | 221 | |
---|
| 222 | if ( cont_fw_new(&self) == -1 ) |
---|
| 223 | return -1; |
---|
| 224 | |
---|
[2683] | 225 | if ( (setup = filter->setup) != NULL ) { |
---|
| 226 | if ( setup(self, codec, filter) == -1 ) { |
---|
| 227 | cont_fw_delete(self); |
---|
| 228 | return -1; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
[2678] | 232 | if ( self->pcb.open != NULL ) { |
---|
| 233 | if ( self->pcb.open(self, codec, info, filter) == -1 ) { |
---|
| 234 | cont_fw_delete(self); |
---|
| 235 | return -1; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | *inst = self; |
---|
| 240 | return 0; |
---|
[2677] | 241 | } |
---|
[2675] | 242 | |
---|
| 243 | int cont_fw_cf_close(CODECFILTER_USERDATA_T inst) { |
---|
| 244 | return cont_fw_delete(inst); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | int cont_fw_cf_pause(CODECFILTER_USERDATA_T inst, int newstate); |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | // no direct read or writing... |
---|
| 251 | int cont_fw_cf_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 252 | return -1; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | int cont_fw_cf_read (CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
| 256 | return -1; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | int cont_fw_cf_flush(CODECFILTER_USERDATA_T inst) { |
---|
| 260 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
| 261 | |
---|
| 262 | if ( self->pcb.flush != NULL ) |
---|
| 263 | return self->pcb.flush(self); |
---|
| 264 | |
---|
| 265 | return 0; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | int cont_fw_cf_delay(CODECFILTER_USERDATA_T inst, uint_least32_t * delay); |
---|
| 269 | |
---|
| 270 | int cont_fw_cf_ctl (CODECFILTER_USERDATA_T inst, int cmd, void * data) { |
---|
[2676] | 271 | struct cont_fw_parent_inst * self = (void*)inst; |
---|
| 272 | int_least32_t type = cmd & ROAR_STREAM_CTL_TYPEMASK; |
---|
| 273 | |
---|
| 274 | cmd -= type; |
---|
| 275 | |
---|
| 276 | ROAR_DBG("cont_fw_cf_ctl(*): command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 277 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
| 278 | |
---|
| 279 | if ( data == NULL && type != ROAR_STREAM_CTL_TYPE_VOID ) |
---|
| 280 | return -1; |
---|
| 281 | |
---|
| 282 | switch (cmd) { |
---|
| 283 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_DELETE): |
---|
| 284 | return 0; |
---|
| 285 | break; |
---|
| 286 | case ROAR_CODECFILTER_CTL2CMD(ROAR_CODECFILTER_CTL_VIRTUAL_NEW): |
---|
| 287 | if ( type != ROAR_STREAM_CTL_TYPE_INT ) |
---|
| 288 | return -1; |
---|
| 289 | |
---|
| 290 | return cont_fw_new_child(self, *(int*)data); |
---|
| 291 | break; |
---|
| 292 | default: |
---|
| 293 | ROAR_DBG("cont_fw_cf_ctl(*): Unknown command: cmd=0x%.8x, type=0x%.8x, pcmd=0x%.8x", |
---|
| 294 | cmd, type, ROAR_CODECFILTER_CTL2CMD(cmd)); |
---|
| 295 | return -1; |
---|
| 296 | } |
---|
| 297 | |
---|
[2675] | 298 | return -1; |
---|
| 299 | } |
---|
| 300 | |
---|
[2661] | 301 | //ll |
---|