[0] | 1 | //streams.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
| 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 | |
---|
[0] | 25 | #include "roard.h" |
---|
| 26 | |
---|
| 27 | int streams_init (void) { |
---|
| 28 | int i; |
---|
| 29 | |
---|
| 30 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 31 | g_streams[i] = NULL; |
---|
| 32 | |
---|
| 33 | return 0; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | int streams_free (void) { |
---|
| 37 | int i; |
---|
| 38 | |
---|
| 39 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 40 | if ( g_streams[i] != NULL ) { |
---|
| 41 | streams_delete(i); |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | return 0; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | int streams_new (void) { |
---|
[16] | 50 | int i, j; |
---|
[494] | 51 | struct roar_stream * n = NULL; |
---|
| 52 | struct roar_stream_server * s = NULL; |
---|
[0] | 53 | |
---|
[1498] | 54 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1155] | 55 | if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state |
---|
[1148] | 56 | return -1; |
---|
[1498] | 57 | #else |
---|
| 58 | if ( g_terminate ) // don't accept new streams in case of termination state |
---|
| 59 | return -1; |
---|
| 60 | #endif |
---|
[1148] | 61 | |
---|
[0] | 62 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 63 | if ( g_streams[i] == NULL ) { |
---|
[494] | 64 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(malloc(sizeof(struct roar_stream_server)))); |
---|
[0] | 65 | if ( n == NULL ) { |
---|
| 66 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
| 67 | ROAR_DBG("streams_new(void) = -1"); |
---|
| 68 | return -1; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | n->id = i; |
---|
| 72 | n->fh = -1; |
---|
| 73 | // n->pos_rel_id = i; |
---|
[1804] | 74 | /* |
---|
[0] | 75 | n->database = NULL; |
---|
| 76 | n->dataoff = NULL; |
---|
| 77 | n->datalen = 0; |
---|
| 78 | n->offset = 0; |
---|
[1804] | 79 | */ |
---|
[381] | 80 | n->pos = 0; |
---|
[0] | 81 | |
---|
[1842] | 82 | s->name = NULL; |
---|
| 83 | |
---|
[494] | 84 | s->client = -1; |
---|
| 85 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
| 86 | s->buffer = NULL; |
---|
| 87 | s->need_extra = 0; |
---|
| 88 | s->output = NULL; |
---|
| 89 | s->is_new = 1; |
---|
| 90 | s->codecfilter = -1; |
---|
[538] | 91 | s->pre_underruns = 0; |
---|
| 92 | s->post_underruns = 0; |
---|
[1137] | 93 | s->delay = 0; |
---|
[538] | 94 | s->codec_orgi = -1; |
---|
[643] | 95 | s->primary = 0; |
---|
[1913] | 96 | s->ready = 0; |
---|
[2151] | 97 | s->outputbuffer = NULL; |
---|
[494] | 98 | |
---|
| 99 | s->mixer.scale = 65535; |
---|
| 100 | s->mixer.rpg_mul = 1; |
---|
| 101 | s->mixer.rpg_div = 1; |
---|
[16] | 102 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
[494] | 103 | s->mixer.mixer[j] = 65535; |
---|
| 104 | |
---|
[1498] | 105 | #ifdef ROAR_SUPPORT_META |
---|
[90] | 106 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
[494] | 107 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
| 108 | s->meta[j].key[0] = 0; |
---|
| 109 | s->meta[j].value = NULL; |
---|
[90] | 110 | } |
---|
[1498] | 111 | #endif |
---|
[0] | 112 | |
---|
[592] | 113 | roar_vio_init_calls(&(s->vio)); |
---|
[930] | 114 | s->driver_id = -1; |
---|
[1030] | 115 | s->flags = ROAR_FLAG_NONE; |
---|
[592] | 116 | |
---|
[1857] | 117 | //roardsp_fchain_init(&(s->fc)); |
---|
[981] | 118 | |
---|
[494] | 119 | g_streams[i] = s; |
---|
[491] | 120 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
[0] | 121 | ROAR_DBG("streams_new(void) = %i", i); |
---|
| 122 | return i; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return -1; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | int streams_delete (int id) { |
---|
[643] | 130 | struct roar_stream_server * s; |
---|
[645] | 131 | int prim; |
---|
[1245] | 132 | int no_vio_close = 0; |
---|
[1836] | 133 | int i; |
---|
[645] | 134 | |
---|
[643] | 135 | if ( (s = g_streams[id]) == NULL ) |
---|
[0] | 136 | return 0; |
---|
| 137 | |
---|
| 138 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
[643] | 139 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
[0] | 140 | |
---|
[1836] | 141 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 142 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
| 143 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 144 | streams_delete(i); |
---|
| 145 | } else { |
---|
| 146 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
[1498] | 151 | #ifdef ROAR_SUPPORT_META |
---|
[1045] | 152 | // delete meta data form other meta streams if needed |
---|
| 153 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
| 154 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
| 155 | stream_meta_clear(id); |
---|
| 156 | stream_meta_finalize(id); |
---|
| 157 | } |
---|
[1498] | 158 | #endif |
---|
[1045] | 159 | |
---|
[643] | 160 | if ( s->codecfilter != -1 ) { |
---|
| 161 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
| 162 | s->codecfilter_inst = NULL; |
---|
| 163 | s->codecfilter = -1; |
---|
| 164 | } |
---|
| 165 | |
---|
[936] | 166 | if ( s->driver_id != -1 ) { |
---|
[937] | 167 | driver_closevio(&(s->vio), s->driver_id); |
---|
[936] | 168 | roar_vio_init_calls(&(s->vio)); |
---|
| 169 | s->driver_id = -1; |
---|
[1245] | 170 | no_vio_close = 1; |
---|
[936] | 171 | } |
---|
| 172 | |
---|
[1857] | 173 | //roardsp_fchain_uninit(&(s->fc)); |
---|
[981] | 174 | |
---|
[643] | 175 | if ( s->client != -1 ) { |
---|
| 176 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
| 177 | client_stream_delete(s->client, id); |
---|
[269] | 178 | } |
---|
| 179 | |
---|
[2151] | 180 | stream_outputbuffer_destroy(id); |
---|
| 181 | |
---|
[643] | 182 | if ( s->buffer != NULL ) |
---|
| 183 | roar_buffer_free(s->buffer); |
---|
| 184 | |
---|
| 185 | if ( s->output != NULL ) |
---|
| 186 | free(s->output); |
---|
| 187 | |
---|
[1243] | 188 | /* |
---|
[643] | 189 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
| 190 | close(ROAR_STREAM(s)->fh); |
---|
[1243] | 191 | */ |
---|
| 192 | |
---|
[1245] | 193 | if ( !no_vio_close ) |
---|
| 194 | roar_vio_close(&(s->vio)); |
---|
[643] | 195 | |
---|
[645] | 196 | prim = s->primary; |
---|
[0] | 197 | |
---|
[1842] | 198 | if ( s->name != NULL ) |
---|
| 199 | free(s->name); |
---|
| 200 | |
---|
[643] | 201 | free(s); |
---|
[0] | 202 | |
---|
| 203 | g_streams[id] = NULL; |
---|
| 204 | |
---|
[645] | 205 | if ( prim ) { |
---|
| 206 | alive = 0; |
---|
| 207 | clean_quit(); |
---|
| 208 | } |
---|
| 209 | |
---|
[0] | 210 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
| 211 | return 0; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | int streams_set_client (int id, int client) { |
---|
| 215 | if ( g_streams[id] == NULL ) |
---|
| 216 | return -1; |
---|
| 217 | |
---|
[491] | 218 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
[0] | 219 | g_streams[id]->client = client; |
---|
| 220 | |
---|
| 221 | return 0; |
---|
| 222 | } |
---|
| 223 | |
---|
[766] | 224 | int streams_get_client (int id) { |
---|
| 225 | if ( g_streams[id] == NULL ) |
---|
| 226 | return -1; |
---|
| 227 | |
---|
| 228 | return g_streams[id]->client; |
---|
| 229 | } |
---|
| 230 | |
---|
[1609] | 231 | int streams_set_dir (int id, int dir, int defaults) { |
---|
| 232 | struct roar_stream_server * ss; |
---|
| 233 | |
---|
| 234 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 235 | return -1; |
---|
| 236 | |
---|
| 237 | ROAR_STREAM(ss)->dir = dir; |
---|
| 238 | |
---|
| 239 | if ( defaults ) { |
---|
| 240 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
| 241 | return -1; |
---|
| 242 | |
---|
[1906] | 243 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%0.4x", dir, g_config->streams[dir].flags); |
---|
| 244 | |
---|
| 245 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
| 246 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
[1609] | 247 | return -1; |
---|
[1906] | 248 | } |
---|
[1609] | 249 | |
---|
| 250 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
| 251 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
| 252 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
| 253 | } |
---|
| 254 | |
---|
[1906] | 255 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
[1609] | 256 | return 0; |
---|
| 257 | } |
---|
[766] | 258 | |
---|
[0] | 259 | int streams_set_fh (int id, int fh) { |
---|
[1243] | 260 | struct roar_stream_server * ss; |
---|
[1913] | 261 | struct roar_stream * s; |
---|
[51] | 262 | int dir; |
---|
| 263 | |
---|
[1913] | 264 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[0] | 265 | return -1; |
---|
| 266 | |
---|
[1913] | 267 | ROAR_DBG("streams_set_fh(id=%i): g_streams[id]->id=%i", id, s->id); |
---|
[491] | 268 | |
---|
[1913] | 269 | s->fh = fh; |
---|
[0] | 270 | |
---|
[1243] | 271 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
| 272 | |
---|
[1610] | 273 | if ( ss->driver_id == -1 && fh != -2 ) |
---|
[1243] | 274 | roar_vio_set_fh(&(ss->vio), fh); |
---|
| 275 | |
---|
| 276 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
[1913] | 277 | s->info.codec, ss) == -1 ) { |
---|
| 278 | return streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here? |
---|
[571] | 279 | } |
---|
[268] | 280 | |
---|
[1610] | 281 | if ( fh == -2 ) { |
---|
| 282 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 283 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
| 284 | fh = -2; |
---|
| 285 | } else { |
---|
| 286 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 287 | if ( fh < 0 ) { |
---|
| 288 | fh = -2; |
---|
| 289 | } else { |
---|
[1913] | 290 | s->fh = fh; |
---|
[1610] | 291 | } |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
[1913] | 296 | ss->ready = 1; |
---|
[634] | 297 | return 0; |
---|
| 298 | } |
---|
| 299 | |
---|
[971] | 300 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
[968] | 301 | |
---|
[1243] | 302 | dir = ROAR_STREAM(ss)->dir; |
---|
[51] | 303 | |
---|
[1912] | 304 | switch (dir) { |
---|
| 305 | case ROAR_DIR_MONITOR: |
---|
| 306 | case ROAR_DIR_RECORD: |
---|
| 307 | case ROAR_DIR_OUTPUT: |
---|
| 308 | case ROAR_DIR_MIDI_OUT: |
---|
| 309 | case ROAR_DIR_LIGHT_OUT: |
---|
| 310 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
| 311 | break; |
---|
[51] | 312 | } |
---|
| 313 | |
---|
| 314 | if ( dir == ROAR_DIR_FILTER ) { |
---|
[1913] | 315 | ss->ready = 1; |
---|
[0] | 316 | return 0; |
---|
| 317 | } else { |
---|
[1913] | 318 | if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 319 | return -1; |
---|
| 320 | |
---|
| 321 | ss->ready = 1; |
---|
| 322 | return 0; |
---|
[0] | 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
[66] | 326 | int streams_get_fh (int id) { |
---|
[84] | 327 | if ( id < 0 ) |
---|
| 328 | return -1; |
---|
| 329 | |
---|
[66] | 330 | if ( g_streams[id] == NULL ) |
---|
| 331 | return -1; |
---|
| 332 | |
---|
[609] | 333 | return ROAR_STREAM(g_streams[id])->fh; |
---|
[66] | 334 | } |
---|
[0] | 335 | |
---|
| 336 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
| 337 | if ( g_streams[id] == NULL ) |
---|
| 338 | return -1; |
---|
| 339 | |
---|
| 340 | *stream = g_streams[id]; |
---|
| 341 | |
---|
| 342 | return 0; |
---|
| 343 | } |
---|
| 344 | |
---|
[377] | 345 | int streams_set_socktype (int id, int socktype) { |
---|
| 346 | if ( g_streams[id] == NULL ) |
---|
| 347 | return -1; |
---|
| 348 | |
---|
| 349 | g_streams[id]->socktype = socktype; |
---|
| 350 | |
---|
| 351 | return 0; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | int streams_get_socktype (int id) { |
---|
| 355 | if ( g_streams[id] == NULL ) |
---|
| 356 | return -1; |
---|
| 357 | |
---|
| 358 | return g_streams[id]->socktype; |
---|
| 359 | } |
---|
[0] | 360 | |
---|
[643] | 361 | int streams_set_primary (int id, int prim) { |
---|
| 362 | if ( g_streams[id] == NULL ) |
---|
| 363 | return -1; |
---|
| 364 | |
---|
| 365 | g_streams[id]->primary = prim; |
---|
| 366 | |
---|
| 367 | return 0; |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | int streams_mark_primary (int id) { |
---|
| 371 | return streams_set_primary(id, 1); |
---|
| 372 | } |
---|
[1029] | 373 | |
---|
[1116] | 374 | int streams_set_sync (int id, int sync) { |
---|
[1117] | 375 | int fh = streams_get_fh(id); |
---|
[1116] | 376 | |
---|
[1117] | 377 | if ( fh != -1 ) { |
---|
| 378 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 379 | return -1; |
---|
[1116] | 380 | |
---|
[1498] | 381 | #ifdef ROAR_FDATASYNC |
---|
[1171] | 382 | ROAR_FDATASYNC(fh); |
---|
[1498] | 383 | #endif |
---|
[1125] | 384 | |
---|
| 385 | return 0; |
---|
[1117] | 386 | } else { |
---|
| 387 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
| 388 | } |
---|
[1116] | 389 | } |
---|
| 390 | |
---|
[1928] | 391 | int streams_set_mmap (int id, int reset) { |
---|
| 392 | int use = !reset; |
---|
| 393 | |
---|
| 394 | if ( g_streams[id] == NULL ) |
---|
| 395 | return -1; |
---|
| 396 | |
---|
| 397 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
| 398 | } |
---|
| 399 | |
---|
[1029] | 400 | int streams_set_flag (int id, int flag) { |
---|
| 401 | if ( g_streams[id] == NULL ) |
---|
| 402 | return -1; |
---|
| 403 | |
---|
[1926] | 404 | if ( flag & ROAR_FLAG_MMAP ) |
---|
[1928] | 405 | if ( streams_set_mmap(id, 0) == -1 ) |
---|
| 406 | flag -= ROAR_FLAG_MMAP; |
---|
[1926] | 407 | |
---|
[1043] | 408 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 409 | streams_set_primary(id, 1); |
---|
| 410 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 411 | } |
---|
| 412 | |
---|
[1116] | 413 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[1908] | 414 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
| 415 | // for this stream types the flag is used in the subsystem: |
---|
| 416 | case ROAR_DIR_BRIDGE: |
---|
| 417 | case ROAR_DIR_MIDI_OUT: |
---|
| 418 | break; |
---|
| 419 | |
---|
| 420 | // normal behavor (vio blocking): |
---|
| 421 | default: |
---|
| 422 | if ( streams_set_sync(id, 1) == -1 ) |
---|
| 423 | flag -= ROAR_FLAG_SYNC; |
---|
| 424 | } |
---|
[1116] | 425 | } |
---|
| 426 | |
---|
[1585] | 427 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
[1590] | 428 | g_streams[id]->flags |= flag; |
---|
| 429 | if ( streams_set_mixer(id) == -1 ) { |
---|
| 430 | g_streams[id]->flags -= flag; |
---|
| 431 | return -1; |
---|
| 432 | } |
---|
[1585] | 433 | } |
---|
| 434 | |
---|
[1029] | 435 | g_streams[id]->flags |= flag; |
---|
| 436 | |
---|
[1498] | 437 | #ifdef ROAR_SUPPORT_META |
---|
[1043] | 438 | if ( flag & ROAR_FLAG_META ) |
---|
| 439 | stream_meta_finalize(id); |
---|
[1498] | 440 | #endif |
---|
[1043] | 441 | |
---|
[1029] | 442 | return 0; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | int streams_reset_flag (int id, int flag) { |
---|
| 446 | if ( g_streams[id] == NULL ) |
---|
| 447 | return -1; |
---|
| 448 | |
---|
[1928] | 449 | if ( flag & ROAR_FLAG_MMAP ) |
---|
| 450 | if ( streams_set_mmap(id, 1) == -1 ) |
---|
| 451 | flag -= ROAR_FLAG_MMAP; |
---|
| 452 | |
---|
[1043] | 453 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 454 | streams_set_primary(id, 0); |
---|
| 455 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 456 | } |
---|
| 457 | |
---|
[1116] | 458 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
| 459 | streams_set_sync(id, 0); |
---|
| 460 | } |
---|
| 461 | |
---|
[1029] | 462 | g_streams[id]->flags |= flag; |
---|
| 463 | g_streams[id]->flags -= flag; |
---|
| 464 | |
---|
| 465 | return 0; |
---|
| 466 | } |
---|
| 467 | |
---|
[1042] | 468 | int streams_get_flag (int id, int flag) { |
---|
| 469 | if ( g_streams[id] == NULL ) |
---|
| 470 | return -1; |
---|
| 471 | |
---|
| 472 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
| 473 | } |
---|
| 474 | |
---|
[1842] | 475 | int streams_set_name (int id, char * name) { |
---|
| 476 | char * str; |
---|
| 477 | |
---|
| 478 | if ( g_streams[id] == NULL ) |
---|
| 479 | return -1; |
---|
| 480 | |
---|
| 481 | if ( (str = strdup(name)) == NULL ) |
---|
| 482 | return -1; |
---|
| 483 | |
---|
| 484 | if ( g_streams[id]->name != NULL ) |
---|
| 485 | free(g_streams[id]->name); |
---|
| 486 | |
---|
| 487 | g_streams[id]->name = str; |
---|
[1845] | 488 | |
---|
| 489 | return 0; |
---|
[1842] | 490 | } |
---|
| 491 | |
---|
| 492 | char * streams_get_name (int id) { |
---|
| 493 | if ( g_streams[id] == NULL ) |
---|
| 494 | return NULL; |
---|
| 495 | |
---|
| 496 | return g_streams[id]->name; |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | |
---|
[1223] | 500 | int streams_calc_delay (int id) { |
---|
[1142] | 501 | struct roar_stream_server * ss; |
---|
[1151] | 502 | struct roar_stream * s; |
---|
[1142] | 503 | register uint_least32_t d = 0; |
---|
| 504 | uint_least32_t t[1]; |
---|
[1151] | 505 | uint64_t tmp; |
---|
[1142] | 506 | |
---|
[1151] | 507 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[1142] | 508 | return -1; |
---|
| 509 | |
---|
| 510 | if ( ss->codecfilter != -1 ) { |
---|
| 511 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
| 512 | d += *t; |
---|
| 513 | } |
---|
| 514 | |
---|
[1151] | 515 | if ( ss->vio.ctl != NULL ) { |
---|
| 516 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
[1223] | 517 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
[1151] | 518 | tmp = *t; |
---|
| 519 | tmp *= 1000000; // musec per sec |
---|
| 520 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
[1223] | 521 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp); |
---|
[1151] | 522 | |
---|
| 523 | d += tmp; |
---|
| 524 | } |
---|
| 525 | } |
---|
| 526 | |
---|
[1223] | 527 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
[1151] | 528 | |
---|
[1142] | 529 | ss->delay = d; |
---|
| 530 | |
---|
| 531 | return 0; |
---|
| 532 | } |
---|
| 533 | |
---|
[1590] | 534 | int streams_set_mixer (int id) { |
---|
| 535 | struct roar_stream_server * ss; |
---|
| 536 | |
---|
| 537 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 538 | return -1; |
---|
| 539 | |
---|
| 540 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
| 541 | return 0; |
---|
| 542 | |
---|
| 543 | if ( ss->driver_id == -1 ) |
---|
| 544 | return 0; |
---|
| 545 | |
---|
| 546 | return driver_set_volume(id, &(ss->mixer)); |
---|
| 547 | } |
---|
| 548 | |
---|
[1224] | 549 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
| 550 | struct roar_stream_server * ss; |
---|
| 551 | int_least32_t comp; |
---|
| 552 | |
---|
| 553 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 554 | return -1; |
---|
| 555 | |
---|
| 556 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
| 557 | |
---|
| 558 | cmd &= ~comp; |
---|
| 559 | |
---|
[1239] | 560 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
[1238] | 561 | |
---|
[1224] | 562 | switch (comp) { |
---|
| 563 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
| 564 | break; |
---|
| 565 | case ROAR_STREAM_CTL_COMP_CF: |
---|
| 566 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
| 567 | break; |
---|
| 568 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
| 569 | break; |
---|
| 570 | default: |
---|
| 571 | return -1; |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | return -1; |
---|
| 575 | } |
---|
| 576 | |
---|
[0] | 577 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
| 578 | if ( g_streams[id] == NULL ) |
---|
| 579 | return -1; |
---|
| 580 | |
---|
| 581 | // output buffer size does never change. |
---|
| 582 | if ( g_streams[id]->output != NULL ) { |
---|
| 583 | *buffer = g_streams[id]->output; |
---|
| 584 | return 0; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
| 588 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
| 589 | return -1; |
---|
| 590 | } |
---|
| 591 | |
---|
| 592 | *buffer = g_streams[id]->output; |
---|
| 593 | |
---|
| 594 | return 0; |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | int streams_fill_mixbuffer (int id, struct roar_audio_info * info) { |
---|
| 598 | // TODO: decide: is this the most complex, hacked, un-understadable, |
---|
| 599 | // un-writeable and even worse: un-readable |
---|
| 600 | // function in the whole project? |
---|
| 601 | size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
[381] | 602 | size_t needed = todo; |
---|
[0] | 603 | size_t todo_in; |
---|
| 604 | size_t len, outlen; |
---|
[491] | 605 | size_t mul = 1, div = 1; |
---|
[0] | 606 | void * rest = NULL; |
---|
| 607 | void * in = NULL; |
---|
| 608 | struct roar_buffer * buf; |
---|
| 609 | struct roar_audio_info * stream_info; |
---|
[495] | 610 | struct roar_stream_server * stream = g_streams[id]; |
---|
[0] | 611 | int is_the_same = 0; |
---|
| 612 | |
---|
| 613 | if ( g_streams[id] == NULL ) |
---|
| 614 | return -1; |
---|
| 615 | |
---|
| 616 | if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) { |
---|
| 617 | return -1; |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | if ( rest == NULL ) { |
---|
| 621 | return -1; |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | // set up stream_info |
---|
| 625 | |
---|
[495] | 626 | stream_info = &(ROAR_STREAM(stream)->info); |
---|
[0] | 627 | |
---|
| 628 | // calc todo_in |
---|
[491] | 629 | todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 630 | |
---|
[2021] | 631 | if ( todo_in == 0 ) { |
---|
| 632 | ROAR_WARN("streams_fill_mixbuffer(id=%i, info=%p{...}): todo_in == 0, this should not happen!", id, info); |
---|
| 633 | return -1; |
---|
| 634 | } |
---|
| 635 | |
---|
[491] | 636 | // calc mul and div: |
---|
| 637 | mul = todo / todo_in; |
---|
| 638 | div = todo_in / todo; |
---|
| 639 | |
---|
| 640 | if ( mul == 0 ) { |
---|
| 641 | mul = 1; |
---|
| 642 | } else { |
---|
| 643 | div = 1; |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div); |
---|
[0] | 647 | |
---|
| 648 | ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo); |
---|
| 649 | // are both (input and output) of same format? |
---|
| 650 | |
---|
| 651 | |
---|
| 652 | ROAR_DBG("streams_fill_mixbuffer(*): stream_info:"); |
---|
| 653 | roar_debug_audio_info_print(stream_info); |
---|
| 654 | ROAR_DBG("streams_fill_mixbuffer(*): info:"); |
---|
| 655 | roar_debug_audio_info_print(info); |
---|
| 656 | |
---|
| 657 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 658 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 659 | |
---|
| 660 | ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same); |
---|
| 661 | |
---|
| 662 | /* How it works: |
---|
| 663 | * |
---|
| 664 | * set a counter to the number of samples we need. |
---|
| 665 | * loop until we have all samples done or no samples are |
---|
| 666 | * left in our input buffer. |
---|
| 667 | * If there a no samples left in the input buffer: fill the rest |
---|
| 668 | * of the output buffer with zeros. |
---|
| 669 | * |
---|
| 670 | * The loop: |
---|
| 671 | * get a buffer from the input. |
---|
| 672 | * if it's bigger than needed, set an offset. |
---|
| 673 | * The rest of the data: |
---|
| 674 | * 0) convert endianness (codec) from remote to local... |
---|
| 675 | * 1) change bits in of the samples |
---|
| 676 | * 2) change sample rate |
---|
| 677 | * 3) change the nummber of channels |
---|
| 678 | * 4) insert into output buffer |
---|
| 679 | */ |
---|
| 680 | |
---|
| 681 | /* |
---|
| 682 | // get our first buffer: |
---|
| 683 | |
---|
| 684 | if ( stream_shift_buffer(id, &buf) == -1 ) { |
---|
| 685 | return -1; |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | // first test for some basic simple cases... |
---|
| 689 | |
---|
| 690 | if ( buf == NULL ) { // we habe nothing in input queue |
---|
| 691 | // we may memset() our output buffer OR |
---|
| 692 | // just return with -1 so we are going to |
---|
| 693 | // be ignored. |
---|
| 694 | return -1; |
---|
| 695 | } |
---|
| 696 | */ |
---|
| 697 | |
---|
| 698 | while (todo) { // main loop |
---|
| 699 | ROAR_DBG("streams_fill_mixbuffer(*): looping..."); |
---|
| 700 | // exit loop if nothing is left, even if we need more data.. |
---|
| 701 | if ( stream_shift_buffer(id, &buf) == -1 ) |
---|
| 702 | break; |
---|
| 703 | if ( buf == NULL ) |
---|
| 704 | break; |
---|
| 705 | |
---|
| 706 | // read the data for this loop... |
---|
| 707 | roar_buffer_get_data(buf, &in); |
---|
| 708 | roar_buffer_get_len(buf, &len); |
---|
| 709 | |
---|
| 710 | ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len); |
---|
| 711 | |
---|
| 712 | if ( len > todo_in ) { |
---|
| 713 | roar_buffer_set_offset(buf, todo_in); |
---|
| 714 | len = todo_in; |
---|
| 715 | } else { |
---|
| 716 | roar_buffer_set_len(buf, 0); // queue for deletation |
---|
| 717 | } |
---|
| 718 | |
---|
| 719 | // we now have 'len' bytes in 'in' |
---|
| 720 | |
---|
| 721 | // calc how much outlen this has... |
---|
[491] | 722 | outlen = (len * mul) / div; |
---|
[0] | 723 | |
---|
| 724 | ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len); |
---|
| 725 | |
---|
| 726 | if ( is_the_same ) { |
---|
| 727 | /* |
---|
| 728 | * 0) convert endianness (codec) from remote to local... |
---|
| 729 | * 1) change bits in of the samples |
---|
| 730 | * 2) change sample rate |
---|
| 731 | * 3) change the nummber of channels |
---|
| 732 | \\==> skiping,... |
---|
| 733 | */ |
---|
| 734 | // * 4) insert into output buffer |
---|
| 735 | ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest); |
---|
| 736 | if ( memcpy(rest, in, len) != rest ) { |
---|
| 737 | ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer."); |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | } else { |
---|
| 741 | |
---|
| 742 | /* |
---|
| 743 | // * 0) convert endianness (codec) from remote to local... |
---|
| 744 | if ( stream_info->codec != info->codec ) { |
---|
| 745 | // we neet to convert... |
---|
| 746 | return -1; |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | // * 1) change bits in of the samples |
---|
| 750 | if ( stream_info->bits != info->bits ) { |
---|
| 751 | return -1; |
---|
| 752 | } |
---|
| 753 | |
---|
| 754 | // * 2) change sample rate |
---|
| 755 | if ( stream_info->rate != info->rate ) { |
---|
| 756 | return -1; |
---|
| 757 | } |
---|
| 758 | |
---|
| 759 | // * 3) change the nummber of channels |
---|
| 760 | if ( stream_info->channels != info->channels ) { |
---|
| 761 | return -1; |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | // * 4) insert into output buffer |
---|
| 765 | */ |
---|
| 766 | // hey! we have roar_conv() :) |
---|
| 767 | |
---|
[491] | 768 | if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) |
---|
| 769 | return -1; |
---|
| 770 | } |
---|
[0] | 771 | |
---|
[1590] | 772 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 773 | if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 ) |
---|
| 774 | return -1; |
---|
| 775 | } |
---|
[17] | 776 | |
---|
[0] | 777 | // we habe outlen bytes more... |
---|
[491] | 778 | todo -= outlen; |
---|
| 779 | rest += outlen; |
---|
| 780 | todo_in -= len; |
---|
[0] | 781 | |
---|
[491] | 782 | roar_buffer_get_len(buf, &len); |
---|
| 783 | ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len); |
---|
| 784 | if ( len == 0 ) { |
---|
| 785 | roar_buffer_delete(buf, NULL); |
---|
| 786 | } else { |
---|
| 787 | stream_unshift_buffer(id, buf); |
---|
[271] | 788 | } |
---|
[0] | 789 | } |
---|
| 790 | |
---|
[271] | 791 | //len = 0; |
---|
| 792 | //roar_buffer_get_len(buf, &len); |
---|
[0] | 793 | |
---|
| 794 | /* |
---|
| 795 | if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers... |
---|
| 796 | stream_unshift_buffer(id, buf); |
---|
| 797 | else |
---|
| 798 | buffer_delete(buf, NULL); |
---|
| 799 | */ |
---|
| 800 | |
---|
[609] | 801 | ROAR_STREAM(g_streams[id])->pos = |
---|
| 802 | ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos, |
---|
[381] | 803 | ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo)); |
---|
| 804 | //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos); |
---|
| 805 | |
---|
[495] | 806 | if ( todo > 0 ) { // zeroize the rest of the buffer |
---|
[0] | 807 | memset(rest, 0, todo); |
---|
[125] | 808 | |
---|
| 809 | if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) { |
---|
[495] | 810 | if ( g_streams[id]->is_new ) { |
---|
| 811 | stream->pre_underruns++; |
---|
| 812 | } else { |
---|
[701] | 813 | ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo); |
---|
[495] | 814 | stream->post_underruns++; |
---|
| 815 | } |
---|
[125] | 816 | |
---|
[495] | 817 | stream->is_new = 0; |
---|
[125] | 818 | } |
---|
| 819 | } else { |
---|
[495] | 820 | stream->is_new = 0; |
---|
[0] | 821 | } |
---|
| 822 | |
---|
| 823 | return 0; |
---|
| 824 | } |
---|
| 825 | |
---|
[2061] | 826 | int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) { |
---|
| 827 | size_t outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
| 828 | void * outdata; |
---|
| 829 | size_t inlen; |
---|
| 830 | size_t inlen_got; |
---|
| 831 | void * indata = NULL; |
---|
[2091] | 832 | size_t buflen; |
---|
| 833 | void * bufdata = NULL; |
---|
| 834 | struct roar_buffer * bufbuf = NULL; |
---|
[2061] | 835 | int is_the_same = 0; |
---|
| 836 | struct roar_audio_info * stream_info; |
---|
| 837 | struct roar_stream * s; |
---|
| 838 | struct roar_stream_server * ss; |
---|
| 839 | |
---|
| 840 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
| 841 | return -1; |
---|
| 842 | |
---|
| 843 | // set up stream_info |
---|
| 844 | stream_info = &(s->info); |
---|
| 845 | |
---|
| 846 | // calc todo_in |
---|
| 847 | inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 848 | |
---|
[2091] | 849 | buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info); |
---|
| 850 | |
---|
[2061] | 851 | if ( inlen == 0 ) { |
---|
| 852 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info); |
---|
| 853 | return -1; |
---|
| 854 | } |
---|
| 855 | |
---|
| 856 | if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) { |
---|
| 857 | return -1; |
---|
| 858 | } |
---|
| 859 | |
---|
| 860 | if ( outdata == NULL ) { |
---|
| 861 | return -1; |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen); |
---|
| 865 | |
---|
| 866 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 867 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 868 | |
---|
| 869 | ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same); |
---|
| 870 | |
---|
[2091] | 871 | if ( !is_the_same && buflen > outlen ) { |
---|
| 872 | /* |
---|
[2061] | 873 | // this is not supported at the moment |
---|
| 874 | memset(outdata, 0, outlen); |
---|
| 875 | return -1; |
---|
[2091] | 876 | */ |
---|
| 877 | |
---|
| 878 | if ( roar_buffer_new(&bufbuf, buflen) == -1 ) |
---|
| 879 | return -1; |
---|
| 880 | |
---|
| 881 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
| 882 | return -1; |
---|
[2102] | 883 | indata = bufdata; |
---|
[2061] | 884 | } else { |
---|
[2091] | 885 | indata = outdata; |
---|
| 886 | bufdata = outdata; |
---|
[2061] | 887 | } |
---|
| 888 | |
---|
| 889 | inlen_got = inlen; |
---|
| 890 | |
---|
[2102] | 891 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 892 | |
---|
[2061] | 893 | if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) { |
---|
| 894 | if ( ss->is_new ) { |
---|
| 895 | ss->pre_underruns++; |
---|
| 896 | } else { |
---|
[2088] | 897 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id); |
---|
[2061] | 898 | ss->post_underruns++; |
---|
| 899 | } |
---|
| 900 | memset(outdata, 0, outlen); |
---|
| 901 | return 0; |
---|
| 902 | } |
---|
| 903 | |
---|
| 904 | if ( ss->is_new ) { |
---|
[2088] | 905 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", id); |
---|
[2061] | 906 | } |
---|
| 907 | |
---|
| 908 | ss->is_new = 0; |
---|
| 909 | |
---|
| 910 | if ( is_the_same ) { |
---|
| 911 | if ( indata != outdata ) |
---|
| 912 | memcpy(outdata, indata, inlen); |
---|
| 913 | |
---|
| 914 | if ( inlen < outlen ) |
---|
| 915 | memset(outdata+inlen, 0, outlen-inlen); |
---|
| 916 | } else { |
---|
[2095] | 917 | // if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) { |
---|
| 918 | if ( roar_conv2(bufdata, indata, inlen, stream_info, info, buflen) == -1 ) { |
---|
[2091] | 919 | if ( bufbuf != NULL ) |
---|
| 920 | roar_buffer_free(bufbuf); |
---|
[2062] | 921 | return -1; |
---|
| 922 | } |
---|
| 923 | |
---|
| 924 | // memset(outdata, 0, outlen); |
---|
[2061] | 925 | } |
---|
| 926 | |
---|
[2091] | 927 | if ( bufbuf != NULL ) { |
---|
| 928 | memcpy(outdata, bufdata, outlen); |
---|
| 929 | roar_buffer_free(bufbuf); |
---|
| 930 | } |
---|
| 931 | |
---|
[2079] | 932 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 933 | if ( change_vol(outdata, info->bits, outdata, 8*outlen / info->bits, info->channels, &(ss->mixer)) == -1 ) |
---|
| 934 | return -1; |
---|
| 935 | } |
---|
| 936 | |
---|
[2062] | 937 | return 0; |
---|
[2061] | 938 | } |
---|
| 939 | |
---|
[491] | 940 | |
---|
[0] | 941 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
[84] | 942 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
[0] | 943 | int i; |
---|
| 944 | int have = 0; |
---|
| 945 | |
---|
| 946 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 947 | if ( g_streams[i] != NULL ) { |
---|
[826] | 948 | if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR ) |
---|
[0] | 949 | continue; |
---|
| 950 | |
---|
| 951 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
| 952 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
| 953 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
| 954 | continue; |
---|
| 955 | } |
---|
[2061] | 956 | if ( streams_fill_mixbuffer2(i, info) == -1 ) { |
---|
[0] | 957 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
| 958 | continue; |
---|
| 959 | } |
---|
| 960 | |
---|
[139] | 961 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
| 962 | |
---|
[0] | 963 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
| 964 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
| 965 | |
---|
[1887] | 966 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
| 967 | have++; // we have a new stream! |
---|
[0] | 968 | } |
---|
| 969 | } |
---|
| 970 | |
---|
| 971 | bufs[have] = NULL; |
---|
[139] | 972 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
[0] | 973 | |
---|
| 974 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
| 975 | |
---|
| 976 | *bufferlist = bufs; |
---|
[547] | 977 | return have; |
---|
[0] | 978 | } |
---|
| 979 | |
---|
| 980 | |
---|
| 981 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
| 982 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 983 | |
---|
| 984 | if ( g_streams[id] == NULL ) |
---|
| 985 | return -1; |
---|
| 986 | |
---|
| 987 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 988 | g_streams[id]->buffer = buf; |
---|
| 989 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
| 990 | return 0; |
---|
| 991 | } |
---|
| 992 | |
---|
| 993 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 994 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
| 995 | } |
---|
| 996 | |
---|
[2061] | 997 | int stream_shift_out_buffer (int id, void * data, size_t * len) { |
---|
| 998 | if ( g_streams[id] == NULL ) |
---|
| 999 | return -1; |
---|
| 1000 | |
---|
| 1001 | if ( g_streams[id]->buffer == NULL ) |
---|
| 1002 | return -1; |
---|
| 1003 | |
---|
| 1004 | return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len); |
---|
| 1005 | } |
---|
| 1006 | |
---|
[0] | 1007 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
| 1008 | struct roar_buffer * next; |
---|
| 1009 | |
---|
| 1010 | if ( g_streams[id] == NULL ) |
---|
| 1011 | return -1; |
---|
| 1012 | |
---|
| 1013 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 1014 | *buf = NULL; |
---|
| 1015 | return 0; |
---|
| 1016 | } |
---|
| 1017 | |
---|
| 1018 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
| 1019 | |
---|
| 1020 | *buf = g_streams[id]->buffer; |
---|
| 1021 | g_streams[id]->buffer = next; |
---|
| 1022 | |
---|
| 1023 | return 0; |
---|
| 1024 | } |
---|
| 1025 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
| 1026 | if ( g_streams[id] == NULL ) |
---|
| 1027 | return -1; |
---|
| 1028 | |
---|
| 1029 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 1030 | g_streams[id]->buffer = buf; |
---|
| 1031 | return 0; |
---|
| 1032 | } |
---|
| 1033 | |
---|
[271] | 1034 | buf->next = NULL; |
---|
| 1035 | |
---|
[0] | 1036 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
| 1037 | |
---|
| 1038 | g_streams[id]->buffer = buf; |
---|
| 1039 | |
---|
| 1040 | return 0; |
---|
| 1041 | } |
---|
| 1042 | |
---|
[2151] | 1043 | int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) { |
---|
| 1044 | register struct roar_stream_server * ss; |
---|
| 1045 | size_t buflen; |
---|
| 1046 | int ret; |
---|
| 1047 | |
---|
| 1048 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1049 | return -1; |
---|
| 1050 | |
---|
| 1051 | if ( buf != NULL ) /* just be be sure */ |
---|
| 1052 | *buf = NULL; |
---|
| 1053 | |
---|
| 1054 | if ( ss->outputbuffer != NULL ) { |
---|
| 1055 | if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) { |
---|
| 1056 | if ( buflen == len ) { |
---|
| 1057 | if ( buf != NULL ) |
---|
| 1058 | *buf = ss->outputbuffer; |
---|
| 1059 | return 0; |
---|
| 1060 | } else if ( buflen > len ) { |
---|
| 1061 | if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) { |
---|
| 1062 | if ( buf != NULL ) |
---|
| 1063 | *buf = ss->outputbuffer; |
---|
| 1064 | return 0; |
---|
| 1065 | } |
---|
| 1066 | } |
---|
| 1067 | } |
---|
| 1068 | |
---|
| 1069 | // if the buffer is not suitable: |
---|
| 1070 | |
---|
| 1071 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 1072 | ss->outputbuffer = NULL; |
---|
| 1073 | if ( ret == -1 ) |
---|
| 1074 | return ret; |
---|
| 1075 | } |
---|
| 1076 | |
---|
| 1077 | if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 ) |
---|
| 1078 | return -1; |
---|
| 1079 | |
---|
| 1080 | if ( buf != NULL ) |
---|
| 1081 | *buf = ss->outputbuffer; |
---|
| 1082 | |
---|
| 1083 | return 0; |
---|
| 1084 | } |
---|
| 1085 | |
---|
| 1086 | int stream_outputbuffer_destroy(int id) { |
---|
| 1087 | int ret; |
---|
| 1088 | register struct roar_stream_server * ss; |
---|
| 1089 | |
---|
| 1090 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1091 | return -1; |
---|
| 1092 | |
---|
| 1093 | if ( ss->outputbuffer != NULL ) { |
---|
| 1094 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 1095 | ss->outputbuffer = NULL; |
---|
| 1096 | return ret; |
---|
| 1097 | } |
---|
| 1098 | |
---|
| 1099 | return 0; |
---|
| 1100 | } |
---|
| 1101 | |
---|
[0] | 1102 | int streams_check (int id) { |
---|
| 1103 | int fh; |
---|
[1837] | 1104 | int i; |
---|
[508] | 1105 | ssize_t req, realreq, done; |
---|
[0] | 1106 | struct roar_stream * s; |
---|
| 1107 | struct roar_stream_server * ss; |
---|
| 1108 | struct roar_buffer * b; |
---|
| 1109 | char * buf; |
---|
| 1110 | |
---|
| 1111 | if ( g_streams[id] == NULL ) |
---|
| 1112 | return -1; |
---|
| 1113 | |
---|
| 1114 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
| 1115 | |
---|
[609] | 1116 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1117 | |
---|
| 1118 | if ( (fh = s->fh) == -1 ) |
---|
| 1119 | return 0; |
---|
| 1120 | |
---|
[1821] | 1121 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
[0] | 1122 | return 0; |
---|
| 1123 | |
---|
[1821] | 1124 | switch (s->dir) { |
---|
| 1125 | case ROAR_DIR_LIGHT_IN: |
---|
| 1126 | return light_check_stream(id); |
---|
| 1127 | break; |
---|
[1845] | 1128 | case ROAR_DIR_MIDI_IN: |
---|
| 1129 | return midi_check_stream(id); |
---|
| 1130 | break; |
---|
[1821] | 1131 | case ROAR_DIR_PLAY: |
---|
| 1132 | case ROAR_DIR_BIDIR: |
---|
| 1133 | break; |
---|
| 1134 | default: |
---|
| 1135 | return 0; |
---|
| 1136 | break; |
---|
| 1137 | } |
---|
[1585] | 1138 | |
---|
[0] | 1139 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
| 1140 | |
---|
| 1141 | req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
| 1142 | req += ss->need_extra; // bytes left we sould get.... |
---|
| 1143 | |
---|
| 1144 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
| 1145 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
| 1146 | ROAR_DBG("streams_check(*) = -1"); |
---|
| 1147 | return -1; |
---|
| 1148 | } |
---|
| 1149 | |
---|
| 1150 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 1151 | |
---|
| 1152 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
| 1153 | |
---|
[270] | 1154 | if ( ss->codecfilter == -1 ) { |
---|
[508] | 1155 | realreq = req; |
---|
| 1156 | /* |
---|
[270] | 1157 | req = read(fh, buf, req); |
---|
[508] | 1158 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
| 1159 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
| 1160 | req += realreq; |
---|
| 1161 | } |
---|
| 1162 | */ |
---|
| 1163 | done = 0; |
---|
| 1164 | while (req > 0 && done != realreq) { |
---|
[881] | 1165 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
[508] | 1166 | done += req; |
---|
| 1167 | } |
---|
| 1168 | req = done; |
---|
[1837] | 1169 | |
---|
| 1170 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 1171 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 1172 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
| 1173 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 1174 | if ( stream_vio_write(i, buf, req) != req ) { |
---|
| 1175 | streams_delete(i); |
---|
| 1176 | } |
---|
| 1177 | } |
---|
| 1178 | } |
---|
| 1179 | } |
---|
[270] | 1180 | } else { |
---|
| 1181 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
| 1182 | } |
---|
| 1183 | |
---|
| 1184 | if ( req > 0 ) { |
---|
[0] | 1185 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
| 1186 | |
---|
| 1187 | roar_buffer_set_len(b, req); |
---|
| 1188 | |
---|
| 1189 | if ( stream_add_buffer(id, b) != -1 ) |
---|
| 1190 | return 0; |
---|
| 1191 | |
---|
| 1192 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
| 1193 | roar_buffer_free(b); |
---|
| 1194 | } else { |
---|
[272] | 1195 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
| 1196 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 1197 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
| 1198 | #else |
---|
| 1199 | if ( errno != EAGAIN ) { |
---|
| 1200 | #endif |
---|
| 1201 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
| 1202 | streams_delete(id); |
---|
| 1203 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
| 1204 | } |
---|
[334] | 1205 | roar_buffer_free(b); |
---|
[0] | 1206 | return 0; |
---|
| 1207 | } |
---|
| 1208 | |
---|
| 1209 | |
---|
| 1210 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
| 1211 | return -1; |
---|
| 1212 | } |
---|
| 1213 | |
---|
| 1214 | |
---|
[2152] | 1215 | #define _return(x) return (x) |
---|
[0] | 1216 | int streams_send_mon (int id) { |
---|
[936] | 1217 | // int fh; |
---|
[0] | 1218 | struct roar_stream * s; |
---|
| 1219 | struct roar_stream_server * ss; |
---|
[2146] | 1220 | struct roar_buffer * bufbuf = NULL; |
---|
[2148] | 1221 | void * ip; |
---|
[1157] | 1222 | void * obuf; |
---|
| 1223 | int olen; |
---|
[2150] | 1224 | int is_the_same = 1; |
---|
| 1225 | int is_vol_eq = 1; |
---|
[1157] | 1226 | ssize_t ret; |
---|
[0] | 1227 | |
---|
| 1228 | if ( g_streams[id] == NULL ) |
---|
| 1229 | return -1; |
---|
| 1230 | |
---|
| 1231 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 1232 | |
---|
[585] | 1233 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
[0] | 1234 | |
---|
[934] | 1235 | /* |
---|
[0] | 1236 | if ( (fh = s->fh) == -1 ) |
---|
| 1237 | return 0; |
---|
[934] | 1238 | */ |
---|
[0] | 1239 | |
---|
[1914] | 1240 | if ( !ss->ready ) |
---|
| 1241 | return 0; |
---|
[930] | 1242 | |
---|
[1585] | 1243 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1244 | return 0; |
---|
| 1245 | |
---|
[1821] | 1246 | switch (s->dir) { |
---|
| 1247 | case ROAR_DIR_LIGHT_OUT: |
---|
| 1248 | return light_send_stream(id); |
---|
| 1249 | break; |
---|
[1845] | 1250 | case ROAR_DIR_MIDI_OUT: |
---|
| 1251 | return midi_send_stream(id); |
---|
| 1252 | break; |
---|
[1821] | 1253 | case ROAR_DIR_OUTPUT: |
---|
| 1254 | if ( g_standby ) |
---|
| 1255 | return 0; |
---|
| 1256 | case ROAR_DIR_MONITOR: |
---|
| 1257 | case ROAR_DIR_BIDIR: |
---|
| 1258 | break; |
---|
| 1259 | |
---|
| 1260 | default: |
---|
| 1261 | return 0; |
---|
| 1262 | break; |
---|
| 1263 | } |
---|
| 1264 | |
---|
| 1265 | |
---|
[1042] | 1266 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
[0] | 1267 | |
---|
[625] | 1268 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
[2147] | 1269 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) |
---|
| 1270 | is_the_same = 0; |
---|
| 1271 | |
---|
[2150] | 1272 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 1273 | is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1; |
---|
| 1274 | } |
---|
| 1275 | |
---|
| 1276 | if ( !is_the_same || !is_vol_eq ) { |
---|
[625] | 1277 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
| 1278 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
[2152] | 1279 | if ( stream_outputbuffer_request(id, &bufbuf, olen) == -1 ) |
---|
[625] | 1280 | return -1; |
---|
| 1281 | |
---|
[2146] | 1282 | if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) { |
---|
| 1283 | _return(-1); |
---|
| 1284 | } |
---|
| 1285 | |
---|
[625] | 1286 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
[2147] | 1287 | } else { |
---|
| 1288 | obuf = g_output_buffer; |
---|
| 1289 | olen = g_output_buffer_len; |
---|
| 1290 | } |
---|
[625] | 1291 | |
---|
[2148] | 1292 | ip = g_output_buffer; |
---|
| 1293 | |
---|
[2150] | 1294 | if ( !is_vol_eq ) { |
---|
| 1295 | if ( change_vol(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) { |
---|
| 1296 | _return(-1); |
---|
| 1297 | } |
---|
| 1298 | |
---|
| 1299 | ip = obuf; |
---|
| 1300 | } |
---|
| 1301 | |
---|
[2147] | 1302 | if ( !is_the_same ) { |
---|
[2148] | 1303 | if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
[2146] | 1304 | _return(-1); |
---|
[625] | 1305 | } |
---|
| 1306 | } |
---|
| 1307 | |
---|
[585] | 1308 | errno = 0; |
---|
| 1309 | |
---|
| 1310 | if ( ss->codecfilter == -1 ) { |
---|
[1042] | 1311 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
[2146] | 1312 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) { |
---|
| 1313 | _return(0); |
---|
| 1314 | } |
---|
[1014] | 1315 | |
---|
[1157] | 1316 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
[981] | 1317 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1318 | _return(0); |
---|
[625] | 1319 | } |
---|
[1157] | 1320 | |
---|
| 1321 | if ( ret > 0 && errno == 0 ) { |
---|
[1231] | 1322 | ROAR_WARN("streams_send_mon(id=%i): Overrun in stream: wrote %i of %i bytes, %i bytes missing", id, (int)ret, olen, olen-(int)ret); |
---|
[1157] | 1323 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
[2146] | 1324 | _return(0); |
---|
[1157] | 1325 | } |
---|
[585] | 1326 | } else { |
---|
[1012] | 1327 | errno = 0; |
---|
[625] | 1328 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
| 1329 | == olen ) { |
---|
[981] | 1330 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1331 | _return(0); |
---|
[585] | 1332 | } else { // we cann't retry on codec filetered streams |
---|
[1012] | 1333 | if ( errno != EAGAIN ) { |
---|
| 1334 | streams_delete(id); |
---|
[2146] | 1335 | _return(-1); |
---|
[1012] | 1336 | } |
---|
[585] | 1337 | } |
---|
| 1338 | } |
---|
[0] | 1339 | |
---|
[129] | 1340 | if ( errno == EAGAIN ) { |
---|
| 1341 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
| 1342 | // make any gapes in any output because of this |
---|
| 1343 | |
---|
[1750] | 1344 | #ifdef ROAR_HAVE_USLEEP |
---|
[129] | 1345 | usleep(100); // 0.1ms |
---|
[1750] | 1346 | #endif |
---|
[129] | 1347 | |
---|
[881] | 1348 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
[981] | 1349 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[2146] | 1350 | _return(0); |
---|
[1015] | 1351 | } else if ( errno == EAGAIN ) { |
---|
| 1352 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
[2146] | 1353 | _return(0); |
---|
[625] | 1354 | } |
---|
[129] | 1355 | } |
---|
| 1356 | |
---|
[0] | 1357 | // ug... error... delete stream! |
---|
| 1358 | |
---|
| 1359 | streams_delete(id); |
---|
| 1360 | |
---|
[2146] | 1361 | _return(-1); |
---|
[0] | 1362 | } |
---|
[2146] | 1363 | #undef _return |
---|
[0] | 1364 | |
---|
| 1365 | int streams_send_filter(int id) { |
---|
| 1366 | int fh; |
---|
[127] | 1367 | int have = 0; |
---|
| 1368 | int len; |
---|
[0] | 1369 | struct roar_stream * s; |
---|
| 1370 | struct roar_stream_server * ss; |
---|
| 1371 | |
---|
| 1372 | if ( g_streams[id] == NULL ) |
---|
| 1373 | return -1; |
---|
| 1374 | |
---|
| 1375 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
| 1376 | |
---|
[609] | 1377 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1378 | |
---|
| 1379 | if ( (fh = s->fh) == -1 ) |
---|
| 1380 | return 0; |
---|
| 1381 | |
---|
| 1382 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
| 1383 | return 0; |
---|
| 1384 | |
---|
[1585] | 1385 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1386 | return 0; |
---|
| 1387 | |
---|
| 1388 | |
---|
[0] | 1389 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
| 1390 | |
---|
[881] | 1391 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
[127] | 1392 | while ( have < g_output_buffer_len ) { |
---|
[881] | 1393 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
[127] | 1394 | streams_delete(id); |
---|
| 1395 | return -1; |
---|
| 1396 | } |
---|
| 1397 | have += len; |
---|
[0] | 1398 | } |
---|
[127] | 1399 | return 0; |
---|
[0] | 1400 | } |
---|
| 1401 | |
---|
| 1402 | // ug... error... delete stream! |
---|
| 1403 | |
---|
| 1404 | streams_delete(id); |
---|
| 1405 | |
---|
| 1406 | return -1; |
---|
| 1407 | } |
---|
| 1408 | |
---|
[596] | 1409 | |
---|
| 1410 | // VIO: |
---|
| 1411 | |
---|
| 1412 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
| 1413 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1414 | |
---|
| 1415 | if ( !s ) |
---|
| 1416 | return -1; |
---|
| 1417 | |
---|
| 1418 | return stream_vio_s_read(s, buf, count); |
---|
| 1419 | } |
---|
| 1420 | |
---|
| 1421 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
| 1422 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1423 | |
---|
| 1424 | if ( !s ) |
---|
| 1425 | return -1; |
---|
| 1426 | |
---|
| 1427 | return stream_vio_s_write(s, buf, count); |
---|
| 1428 | } |
---|
| 1429 | |
---|
| 1430 | |
---|
| 1431 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[741] | 1432 | size_t len = 0; |
---|
| 1433 | ssize_t r = -1; |
---|
[739] | 1434 | |
---|
[596] | 1435 | errno = 0; |
---|
| 1436 | |
---|
| 1437 | if ( !stream ) |
---|
| 1438 | return -1; |
---|
| 1439 | |
---|
[1613] | 1440 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[881] | 1441 | |
---|
[596] | 1442 | if ( ! stream->vio.read ) |
---|
| 1443 | return -1; |
---|
| 1444 | |
---|
[881] | 1445 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
[739] | 1446 | len += r; |
---|
| 1447 | buf += r; |
---|
| 1448 | count -= r; |
---|
| 1449 | if ( count == 0 ) |
---|
| 1450 | break; |
---|
| 1451 | } |
---|
| 1452 | |
---|
[740] | 1453 | if ( len == 0 && r == -1 ) |
---|
| 1454 | return -1; |
---|
| 1455 | |
---|
[739] | 1456 | return len; |
---|
[596] | 1457 | } |
---|
| 1458 | |
---|
| 1459 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
| 1460 | errno = 0; |
---|
| 1461 | |
---|
| 1462 | if ( !stream ) |
---|
| 1463 | return -1; |
---|
| 1464 | |
---|
[1613] | 1465 | /* |
---|
[934] | 1466 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
| 1467 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[1613] | 1468 | */ |
---|
[934] | 1469 | |
---|
| 1470 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
[596] | 1471 | |
---|
[881] | 1472 | return roar_vio_write(&(stream->vio), buf, count); |
---|
[596] | 1473 | } |
---|
| 1474 | |
---|
[0] | 1475 | //ll |
---|