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