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