[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 | |
---|
[1928] | 388 | int streams_set_mmap (int id, int reset) { |
---|
| 389 | int use = !reset; |
---|
| 390 | |
---|
| 391 | if ( g_streams[id] == NULL ) |
---|
| 392 | return -1; |
---|
| 393 | |
---|
| 394 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
| 395 | } |
---|
| 396 | |
---|
[1029] | 397 | int streams_set_flag (int id, int flag) { |
---|
| 398 | if ( g_streams[id] == NULL ) |
---|
| 399 | return -1; |
---|
| 400 | |
---|
[1926] | 401 | if ( flag & ROAR_FLAG_MMAP ) |
---|
[1928] | 402 | if ( streams_set_mmap(id, 0) == -1 ) |
---|
| 403 | flag -= ROAR_FLAG_MMAP; |
---|
[1926] | 404 | |
---|
[1043] | 405 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 406 | streams_set_primary(id, 1); |
---|
| 407 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 408 | } |
---|
| 409 | |
---|
[1116] | 410 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[1908] | 411 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
| 412 | // for this stream types the flag is used in the subsystem: |
---|
| 413 | case ROAR_DIR_BRIDGE: |
---|
| 414 | case ROAR_DIR_MIDI_OUT: |
---|
| 415 | break; |
---|
| 416 | |
---|
| 417 | // normal behavor (vio blocking): |
---|
| 418 | default: |
---|
| 419 | if ( streams_set_sync(id, 1) == -1 ) |
---|
| 420 | flag -= ROAR_FLAG_SYNC; |
---|
| 421 | } |
---|
[1116] | 422 | } |
---|
| 423 | |
---|
[1585] | 424 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
[1590] | 425 | g_streams[id]->flags |= flag; |
---|
| 426 | if ( streams_set_mixer(id) == -1 ) { |
---|
| 427 | g_streams[id]->flags -= flag; |
---|
| 428 | return -1; |
---|
| 429 | } |
---|
[1585] | 430 | } |
---|
| 431 | |
---|
[1029] | 432 | g_streams[id]->flags |= flag; |
---|
| 433 | |
---|
[1498] | 434 | #ifdef ROAR_SUPPORT_META |
---|
[1043] | 435 | if ( flag & ROAR_FLAG_META ) |
---|
| 436 | stream_meta_finalize(id); |
---|
[1498] | 437 | #endif |
---|
[1043] | 438 | |
---|
[1029] | 439 | return 0; |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | int streams_reset_flag (int id, int flag) { |
---|
| 443 | if ( g_streams[id] == NULL ) |
---|
| 444 | return -1; |
---|
| 445 | |
---|
[1928] | 446 | if ( flag & ROAR_FLAG_MMAP ) |
---|
| 447 | if ( streams_set_mmap(id, 1) == -1 ) |
---|
| 448 | flag -= ROAR_FLAG_MMAP; |
---|
| 449 | |
---|
[1043] | 450 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 451 | streams_set_primary(id, 0); |
---|
| 452 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 453 | } |
---|
| 454 | |
---|
[1116] | 455 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
| 456 | streams_set_sync(id, 0); |
---|
| 457 | } |
---|
| 458 | |
---|
[1029] | 459 | g_streams[id]->flags |= flag; |
---|
| 460 | g_streams[id]->flags -= flag; |
---|
| 461 | |
---|
| 462 | return 0; |
---|
| 463 | } |
---|
| 464 | |
---|
[1042] | 465 | int streams_get_flag (int id, int flag) { |
---|
| 466 | if ( g_streams[id] == NULL ) |
---|
| 467 | return -1; |
---|
| 468 | |
---|
| 469 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
| 470 | } |
---|
| 471 | |
---|
[1842] | 472 | int streams_set_name (int id, char * name) { |
---|
| 473 | char * str; |
---|
| 474 | |
---|
| 475 | if ( g_streams[id] == NULL ) |
---|
| 476 | return -1; |
---|
| 477 | |
---|
| 478 | if ( (str = strdup(name)) == NULL ) |
---|
| 479 | return -1; |
---|
| 480 | |
---|
| 481 | if ( g_streams[id]->name != NULL ) |
---|
| 482 | free(g_streams[id]->name); |
---|
| 483 | |
---|
| 484 | g_streams[id]->name = str; |
---|
[1845] | 485 | |
---|
| 486 | return 0; |
---|
[1842] | 487 | } |
---|
| 488 | |
---|
| 489 | char * streams_get_name (int id) { |
---|
| 490 | if ( g_streams[id] == NULL ) |
---|
| 491 | return NULL; |
---|
| 492 | |
---|
| 493 | return g_streams[id]->name; |
---|
| 494 | } |
---|
| 495 | |
---|
| 496 | |
---|
[1223] | 497 | int streams_calc_delay (int id) { |
---|
[1142] | 498 | struct roar_stream_server * ss; |
---|
[1151] | 499 | struct roar_stream * s; |
---|
[1142] | 500 | register uint_least32_t d = 0; |
---|
| 501 | uint_least32_t t[1]; |
---|
[1151] | 502 | uint64_t tmp; |
---|
[1142] | 503 | |
---|
[1151] | 504 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[1142] | 505 | return -1; |
---|
| 506 | |
---|
| 507 | if ( ss->codecfilter != -1 ) { |
---|
| 508 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
| 509 | d += *t; |
---|
| 510 | } |
---|
| 511 | |
---|
[1151] | 512 | if ( ss->vio.ctl != NULL ) { |
---|
| 513 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
[1223] | 514 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
[1151] | 515 | tmp = *t; |
---|
| 516 | tmp *= 1000000; // musec per sec |
---|
| 517 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
[1223] | 518 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %i", id, tmp); |
---|
[1151] | 519 | |
---|
| 520 | d += tmp; |
---|
| 521 | } |
---|
| 522 | } |
---|
| 523 | |
---|
[1223] | 524 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
[1151] | 525 | |
---|
[1142] | 526 | ss->delay = d; |
---|
| 527 | |
---|
| 528 | return 0; |
---|
| 529 | } |
---|
| 530 | |
---|
[1590] | 531 | int streams_set_mixer (int id) { |
---|
| 532 | struct roar_stream_server * ss; |
---|
| 533 | |
---|
| 534 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 535 | return -1; |
---|
| 536 | |
---|
| 537 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
| 538 | return 0; |
---|
| 539 | |
---|
| 540 | if ( ss->driver_id == -1 ) |
---|
| 541 | return 0; |
---|
| 542 | |
---|
| 543 | return driver_set_volume(id, &(ss->mixer)); |
---|
| 544 | } |
---|
| 545 | |
---|
[1224] | 546 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
| 547 | struct roar_stream_server * ss; |
---|
| 548 | int_least32_t comp; |
---|
| 549 | |
---|
| 550 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 551 | return -1; |
---|
| 552 | |
---|
| 553 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
| 554 | |
---|
| 555 | cmd &= ~comp; |
---|
| 556 | |
---|
[1239] | 557 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
[1238] | 558 | |
---|
[1224] | 559 | switch (comp) { |
---|
| 560 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
| 561 | break; |
---|
| 562 | case ROAR_STREAM_CTL_COMP_CF: |
---|
| 563 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
| 564 | break; |
---|
| 565 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
| 566 | break; |
---|
| 567 | default: |
---|
| 568 | return -1; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | return -1; |
---|
| 572 | } |
---|
| 573 | |
---|
[0] | 574 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
| 575 | if ( g_streams[id] == NULL ) |
---|
| 576 | return -1; |
---|
| 577 | |
---|
| 578 | // output buffer size does never change. |
---|
| 579 | if ( g_streams[id]->output != NULL ) { |
---|
| 580 | *buffer = g_streams[id]->output; |
---|
| 581 | return 0; |
---|
| 582 | } |
---|
| 583 | |
---|
| 584 | if ( (g_streams[id]->output = malloc(size)) == NULL ) { |
---|
| 585 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
| 586 | return -1; |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | *buffer = g_streams[id]->output; |
---|
| 590 | |
---|
| 591 | return 0; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | int streams_fill_mixbuffer (int id, struct roar_audio_info * info) { |
---|
| 595 | // TODO: decide: is this the most complex, hacked, un-understadable, |
---|
| 596 | // un-writeable and even worse: un-readable |
---|
| 597 | // function in the whole project? |
---|
| 598 | size_t todo = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
[381] | 599 | size_t needed = todo; |
---|
[0] | 600 | size_t todo_in; |
---|
| 601 | size_t len, outlen; |
---|
[491] | 602 | size_t mul = 1, div = 1; |
---|
[0] | 603 | void * rest = NULL; |
---|
| 604 | void * in = NULL; |
---|
| 605 | struct roar_buffer * buf; |
---|
| 606 | struct roar_audio_info * stream_info; |
---|
[495] | 607 | struct roar_stream_server * stream = g_streams[id]; |
---|
[0] | 608 | int is_the_same = 0; |
---|
| 609 | |
---|
| 610 | if ( g_streams[id] == NULL ) |
---|
| 611 | return -1; |
---|
| 612 | |
---|
| 613 | if ( streams_get_outputbuffer(id, &rest, todo) == -1 ) { |
---|
| 614 | return -1; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | if ( rest == NULL ) { |
---|
| 618 | return -1; |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | // set up stream_info |
---|
| 622 | |
---|
[495] | 623 | stream_info = &(ROAR_STREAM(stream)->info); |
---|
[0] | 624 | |
---|
| 625 | // calc todo_in |
---|
[491] | 626 | todo_in = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 627 | |
---|
| 628 | // calc mul and div: |
---|
| 629 | mul = todo / todo_in; |
---|
| 630 | div = todo_in / todo; |
---|
| 631 | |
---|
| 632 | if ( mul == 0 ) { |
---|
| 633 | mul = 1; |
---|
| 634 | } else { |
---|
| 635 | div = 1; |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | ROAR_DBG("streams_fill_mixbuffer(*): mul=%i, div=%i", mul, div); |
---|
[0] | 639 | |
---|
| 640 | ROAR_DBG("streams_fill_mixbuffer(*): rest=%p, todo=%i->%i (in->out)", rest, todo_in, todo); |
---|
| 641 | // are both (input and output) of same format? |
---|
| 642 | |
---|
| 643 | |
---|
| 644 | ROAR_DBG("streams_fill_mixbuffer(*): stream_info:"); |
---|
| 645 | roar_debug_audio_info_print(stream_info); |
---|
| 646 | ROAR_DBG("streams_fill_mixbuffer(*): info:"); |
---|
| 647 | roar_debug_audio_info_print(info); |
---|
| 648 | |
---|
| 649 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 650 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 651 | |
---|
| 652 | ROAR_DBG("streams_fill_mixbuffer(*): is_the_same=%i", is_the_same); |
---|
| 653 | |
---|
| 654 | /* How it works: |
---|
| 655 | * |
---|
| 656 | * set a counter to the number of samples we need. |
---|
| 657 | * loop until we have all samples done or no samples are |
---|
| 658 | * left in our input buffer. |
---|
| 659 | * If there a no samples left in the input buffer: fill the rest |
---|
| 660 | * of the output buffer with zeros. |
---|
| 661 | * |
---|
| 662 | * The loop: |
---|
| 663 | * get a buffer from the input. |
---|
| 664 | * if it's bigger than needed, set an offset. |
---|
| 665 | * The rest of the data: |
---|
| 666 | * 0) convert endianness (codec) from remote to local... |
---|
| 667 | * 1) change bits in of the samples |
---|
| 668 | * 2) change sample rate |
---|
| 669 | * 3) change the nummber of channels |
---|
| 670 | * 4) insert into output buffer |
---|
| 671 | */ |
---|
| 672 | |
---|
| 673 | /* |
---|
| 674 | // get our first buffer: |
---|
| 675 | |
---|
| 676 | if ( stream_shift_buffer(id, &buf) == -1 ) { |
---|
| 677 | return -1; |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | // first test for some basic simple cases... |
---|
| 681 | |
---|
| 682 | if ( buf == NULL ) { // we habe nothing in input queue |
---|
| 683 | // we may memset() our output buffer OR |
---|
| 684 | // just return with -1 so we are going to |
---|
| 685 | // be ignored. |
---|
| 686 | return -1; |
---|
| 687 | } |
---|
| 688 | */ |
---|
| 689 | |
---|
| 690 | while (todo) { // main loop |
---|
| 691 | ROAR_DBG("streams_fill_mixbuffer(*): looping..."); |
---|
| 692 | // exit loop if nothing is left, even if we need more data.. |
---|
| 693 | if ( stream_shift_buffer(id, &buf) == -1 ) |
---|
| 694 | break; |
---|
| 695 | if ( buf == NULL ) |
---|
| 696 | break; |
---|
| 697 | |
---|
| 698 | // read the data for this loop... |
---|
| 699 | roar_buffer_get_data(buf, &in); |
---|
| 700 | roar_buffer_get_len(buf, &len); |
---|
| 701 | |
---|
| 702 | ROAR_DBG("streams_fill_mixbuffer(*): len = %i", len); |
---|
| 703 | |
---|
| 704 | if ( len > todo_in ) { |
---|
| 705 | roar_buffer_set_offset(buf, todo_in); |
---|
| 706 | len = todo_in; |
---|
| 707 | } else { |
---|
| 708 | roar_buffer_set_len(buf, 0); // queue for deletation |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | // we now have 'len' bytes in 'in' |
---|
| 712 | |
---|
| 713 | // calc how much outlen this has... |
---|
[491] | 714 | outlen = (len * mul) / div; |
---|
[0] | 715 | |
---|
| 716 | ROAR_DBG("streams_fill_mixbuffer(*): outlen = %i, buf = %p, len = %i", outlen, in, len); |
---|
| 717 | |
---|
| 718 | if ( is_the_same ) { |
---|
| 719 | /* |
---|
| 720 | * 0) convert endianness (codec) from remote to local... |
---|
| 721 | * 1) change bits in of the samples |
---|
| 722 | * 2) change sample rate |
---|
| 723 | * 3) change the nummber of channels |
---|
| 724 | \\==> skiping,... |
---|
| 725 | */ |
---|
| 726 | // * 4) insert into output buffer |
---|
| 727 | ROAR_DBG("streams_fill_mixbuffer(*): memcpy: in->rest: %p -> %p", in, rest); |
---|
| 728 | if ( memcpy(rest, in, len) != rest ) { |
---|
| 729 | ROAR_ERR("streams_fill_mixbuffer(*): memcpy returned invalid pointer."); |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | } else { |
---|
| 733 | |
---|
| 734 | /* |
---|
| 735 | // * 0) convert endianness (codec) from remote to local... |
---|
| 736 | if ( stream_info->codec != info->codec ) { |
---|
| 737 | // we neet to convert... |
---|
| 738 | return -1; |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | // * 1) change bits in of the samples |
---|
| 742 | if ( stream_info->bits != info->bits ) { |
---|
| 743 | return -1; |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | // * 2) change sample rate |
---|
| 747 | if ( stream_info->rate != info->rate ) { |
---|
| 748 | return -1; |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | // * 3) change the nummber of channels |
---|
| 752 | if ( stream_info->channels != info->channels ) { |
---|
| 753 | return -1; |
---|
| 754 | } |
---|
| 755 | |
---|
| 756 | // * 4) insert into output buffer |
---|
| 757 | */ |
---|
| 758 | // hey! we have roar_conv() :) |
---|
| 759 | |
---|
[491] | 760 | if ( roar_conv(rest, in, 8*len / stream_info->bits, stream_info, info) == -1 ) |
---|
| 761 | return -1; |
---|
| 762 | } |
---|
[0] | 763 | |
---|
[1590] | 764 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 765 | if ( change_vol(rest, info->bits, rest, 8*outlen / info->bits, info->channels, &(stream->mixer)) == -1 ) |
---|
| 766 | return -1; |
---|
| 767 | } |
---|
[17] | 768 | |
---|
[0] | 769 | // we habe outlen bytes more... |
---|
[491] | 770 | todo -= outlen; |
---|
| 771 | rest += outlen; |
---|
| 772 | todo_in -= len; |
---|
[0] | 773 | |
---|
[491] | 774 | roar_buffer_get_len(buf, &len); |
---|
| 775 | ROAR_DBG("streams_fill_mixbuffer(*): New length of buffer %p is %i", buf, len); |
---|
| 776 | if ( len == 0 ) { |
---|
| 777 | roar_buffer_delete(buf, NULL); |
---|
| 778 | } else { |
---|
| 779 | stream_unshift_buffer(id, buf); |
---|
[271] | 780 | } |
---|
[0] | 781 | } |
---|
| 782 | |
---|
[271] | 783 | //len = 0; |
---|
| 784 | //roar_buffer_get_len(buf, &len); |
---|
[0] | 785 | |
---|
| 786 | /* |
---|
| 787 | if ( len > 0 ) // we still have some data in this buffer, re-inserting it to the input buffers... |
---|
| 788 | stream_unshift_buffer(id, buf); |
---|
| 789 | else |
---|
| 790 | buffer_delete(buf, NULL); |
---|
| 791 | */ |
---|
| 792 | |
---|
[609] | 793 | ROAR_STREAM(g_streams[id])->pos = |
---|
| 794 | ROAR_MATH_OVERFLOW_ADD(ROAR_STREAM(g_streams[id])->pos, |
---|
[381] | 795 | ROAR_OUTPUT_CALC_OUTBUFSAMP(info, needed-todo)); |
---|
| 796 | //ROAR_WARN("stream=%i, pos=%u", id, ((struct roar_stream*)g_streams[id])->pos); |
---|
| 797 | |
---|
[495] | 798 | if ( todo > 0 ) { // zeroize the rest of the buffer |
---|
[0] | 799 | memset(rest, 0, todo); |
---|
[125] | 800 | |
---|
| 801 | if ( todo != ROAR_OUTPUT_CALC_OUTBUFSIZE(info) ) { |
---|
[495] | 802 | if ( g_streams[id]->is_new ) { |
---|
| 803 | stream->pre_underruns++; |
---|
| 804 | } else { |
---|
[701] | 805 | ROAR_WARN("streams_fill_mixbuffer(*): Underrun in stream: %u bytes missing, filling with zeros", (unsigned int)todo); |
---|
[495] | 806 | stream->post_underruns++; |
---|
| 807 | } |
---|
[125] | 808 | |
---|
[495] | 809 | stream->is_new = 0; |
---|
[125] | 810 | } |
---|
| 811 | } else { |
---|
[495] | 812 | stream->is_new = 0; |
---|
[0] | 813 | } |
---|
| 814 | |
---|
| 815 | return 0; |
---|
| 816 | } |
---|
| 817 | |
---|
[491] | 818 | |
---|
[0] | 819 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
[84] | 820 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
[0] | 821 | int i; |
---|
| 822 | int have = 0; |
---|
| 823 | |
---|
| 824 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 825 | if ( g_streams[i] != NULL ) { |
---|
[826] | 826 | if ( ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_PLAY && ROAR_STREAM(g_streams[i])->dir != ROAR_DIR_BIDIR ) |
---|
[0] | 827 | continue; |
---|
| 828 | |
---|
| 829 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
| 830 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
| 831 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
| 832 | continue; |
---|
| 833 | } |
---|
| 834 | if ( streams_fill_mixbuffer(i, info) == -1 ) { |
---|
| 835 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
| 836 | continue; |
---|
| 837 | } |
---|
| 838 | |
---|
[139] | 839 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
| 840 | |
---|
[0] | 841 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
| 842 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
| 843 | |
---|
[1887] | 844 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
| 845 | have++; // we have a new stream! |
---|
[0] | 846 | } |
---|
| 847 | } |
---|
| 848 | |
---|
| 849 | bufs[have] = NULL; |
---|
[139] | 850 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
[0] | 851 | |
---|
| 852 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
| 853 | |
---|
| 854 | *bufferlist = bufs; |
---|
[547] | 855 | return have; |
---|
[0] | 856 | } |
---|
| 857 | |
---|
| 858 | |
---|
| 859 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
| 860 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 861 | |
---|
| 862 | if ( g_streams[id] == NULL ) |
---|
| 863 | return -1; |
---|
| 864 | |
---|
| 865 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 866 | g_streams[id]->buffer = buf; |
---|
| 867 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
| 868 | return 0; |
---|
| 869 | } |
---|
| 870 | |
---|
| 871 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 872 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
| 873 | } |
---|
| 874 | |
---|
| 875 | int stream_shift_buffer (int id, struct roar_buffer ** buf) { |
---|
| 876 | struct roar_buffer * next; |
---|
| 877 | |
---|
| 878 | if ( g_streams[id] == NULL ) |
---|
| 879 | return -1; |
---|
| 880 | |
---|
| 881 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 882 | *buf = NULL; |
---|
| 883 | return 0; |
---|
| 884 | } |
---|
| 885 | |
---|
| 886 | roar_buffer_get_next(g_streams[id]->buffer, &next); |
---|
| 887 | |
---|
| 888 | *buf = g_streams[id]->buffer; |
---|
| 889 | g_streams[id]->buffer = next; |
---|
| 890 | |
---|
| 891 | return 0; |
---|
| 892 | } |
---|
| 893 | int stream_unshift_buffer (int id, struct roar_buffer * buf) { |
---|
| 894 | if ( g_streams[id] == NULL ) |
---|
| 895 | return -1; |
---|
| 896 | |
---|
| 897 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 898 | g_streams[id]->buffer = buf; |
---|
| 899 | return 0; |
---|
| 900 | } |
---|
| 901 | |
---|
[271] | 902 | buf->next = NULL; |
---|
| 903 | |
---|
[0] | 904 | roar_buffer_add(buf, g_streams[id]->buffer); |
---|
| 905 | |
---|
| 906 | g_streams[id]->buffer = buf; |
---|
| 907 | |
---|
| 908 | return 0; |
---|
| 909 | } |
---|
| 910 | |
---|
| 911 | int streams_check (int id) { |
---|
| 912 | int fh; |
---|
[1837] | 913 | int i; |
---|
[508] | 914 | ssize_t req, realreq, done; |
---|
[0] | 915 | struct roar_stream * s; |
---|
| 916 | struct roar_stream_server * ss; |
---|
| 917 | struct roar_buffer * b; |
---|
| 918 | char * buf; |
---|
| 919 | |
---|
| 920 | if ( g_streams[id] == NULL ) |
---|
| 921 | return -1; |
---|
| 922 | |
---|
| 923 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
| 924 | |
---|
[609] | 925 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 926 | |
---|
| 927 | if ( (fh = s->fh) == -1 ) |
---|
| 928 | return 0; |
---|
| 929 | |
---|
[1821] | 930 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
[0] | 931 | return 0; |
---|
| 932 | |
---|
[1821] | 933 | switch (s->dir) { |
---|
| 934 | case ROAR_DIR_LIGHT_IN: |
---|
| 935 | return light_check_stream(id); |
---|
| 936 | break; |
---|
[1845] | 937 | case ROAR_DIR_MIDI_IN: |
---|
| 938 | return midi_check_stream(id); |
---|
| 939 | break; |
---|
[1821] | 940 | case ROAR_DIR_PLAY: |
---|
| 941 | case ROAR_DIR_BIDIR: |
---|
| 942 | break; |
---|
| 943 | default: |
---|
| 944 | return 0; |
---|
| 945 | break; |
---|
| 946 | } |
---|
[1585] | 947 | |
---|
[0] | 948 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
| 949 | |
---|
| 950 | req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * s->info.bits / 8; // optimal size |
---|
| 951 | req += ss->need_extra; // bytes left we sould get.... |
---|
| 952 | |
---|
| 953 | if ( roar_buffer_new(&b, req) == -1 ) { |
---|
| 954 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
| 955 | ROAR_DBG("streams_check(*) = -1"); |
---|
| 956 | return -1; |
---|
| 957 | } |
---|
| 958 | |
---|
| 959 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 960 | |
---|
| 961 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
| 962 | |
---|
[270] | 963 | if ( ss->codecfilter == -1 ) { |
---|
[508] | 964 | realreq = req; |
---|
| 965 | /* |
---|
[270] | 966 | req = read(fh, buf, req); |
---|
[508] | 967 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
| 968 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
| 969 | req += realreq; |
---|
| 970 | } |
---|
| 971 | */ |
---|
| 972 | done = 0; |
---|
| 973 | while (req > 0 && done != realreq) { |
---|
[881] | 974 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
[508] | 975 | done += req; |
---|
| 976 | } |
---|
| 977 | req = done; |
---|
[1837] | 978 | |
---|
| 979 | roar_buffer_get_data(b, (void **)&buf); |
---|
| 980 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 981 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
| 982 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 983 | if ( stream_vio_write(i, buf, req) != req ) { |
---|
| 984 | streams_delete(i); |
---|
| 985 | } |
---|
| 986 | } |
---|
| 987 | } |
---|
| 988 | } |
---|
[270] | 989 | } else { |
---|
| 990 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
| 991 | } |
---|
| 992 | |
---|
| 993 | if ( req > 0 ) { |
---|
[0] | 994 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
| 995 | |
---|
| 996 | roar_buffer_set_len(b, req); |
---|
| 997 | |
---|
| 998 | if ( stream_add_buffer(id, b) != -1 ) |
---|
| 999 | return 0; |
---|
| 1000 | |
---|
| 1001 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
| 1002 | roar_buffer_free(b); |
---|
| 1003 | } else { |
---|
[272] | 1004 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
| 1005 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 1006 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
| 1007 | #else |
---|
| 1008 | if ( errno != EAGAIN ) { |
---|
| 1009 | #endif |
---|
| 1010 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
| 1011 | streams_delete(id); |
---|
| 1012 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
| 1013 | } |
---|
[334] | 1014 | roar_buffer_free(b); |
---|
[0] | 1015 | return 0; |
---|
| 1016 | } |
---|
| 1017 | |
---|
| 1018 | |
---|
| 1019 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
| 1020 | return -1; |
---|
| 1021 | } |
---|
| 1022 | |
---|
| 1023 | |
---|
| 1024 | int streams_send_mon (int id) { |
---|
[936] | 1025 | // int fh; |
---|
[0] | 1026 | struct roar_stream * s; |
---|
| 1027 | struct roar_stream_server * ss; |
---|
[1157] | 1028 | void * obuf; |
---|
| 1029 | int olen; |
---|
| 1030 | int need_to_free = 0; |
---|
| 1031 | ssize_t ret; |
---|
[0] | 1032 | |
---|
| 1033 | if ( g_streams[id] == NULL ) |
---|
| 1034 | return -1; |
---|
| 1035 | |
---|
| 1036 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 1037 | |
---|
[585] | 1038 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
[0] | 1039 | |
---|
[934] | 1040 | /* |
---|
[0] | 1041 | if ( (fh = s->fh) == -1 ) |
---|
| 1042 | return 0; |
---|
[934] | 1043 | */ |
---|
[0] | 1044 | |
---|
[1914] | 1045 | if ( !ss->ready ) |
---|
| 1046 | return 0; |
---|
[930] | 1047 | |
---|
[1585] | 1048 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1049 | return 0; |
---|
| 1050 | |
---|
[1821] | 1051 | switch (s->dir) { |
---|
| 1052 | case ROAR_DIR_LIGHT_OUT: |
---|
| 1053 | return light_send_stream(id); |
---|
| 1054 | break; |
---|
[1845] | 1055 | case ROAR_DIR_MIDI_OUT: |
---|
| 1056 | return midi_send_stream(id); |
---|
| 1057 | break; |
---|
[1821] | 1058 | case ROAR_DIR_OUTPUT: |
---|
| 1059 | if ( g_standby ) |
---|
| 1060 | return 0; |
---|
| 1061 | case ROAR_DIR_MONITOR: |
---|
| 1062 | case ROAR_DIR_BIDIR: |
---|
| 1063 | break; |
---|
| 1064 | |
---|
| 1065 | default: |
---|
| 1066 | return 0; |
---|
| 1067 | break; |
---|
| 1068 | } |
---|
| 1069 | |
---|
| 1070 | |
---|
[1042] | 1071 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
[0] | 1072 | |
---|
[625] | 1073 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
| 1074 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) { |
---|
| 1075 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
| 1076 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
| 1077 | if ( (obuf = malloc(olen)) == NULL ) |
---|
| 1078 | return -1; |
---|
| 1079 | |
---|
| 1080 | need_to_free = 1; |
---|
| 1081 | |
---|
| 1082 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
| 1083 | |
---|
| 1084 | if ( roar_conv(obuf, g_output_buffer, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
| 1085 | free(obuf); |
---|
| 1086 | return -1; |
---|
| 1087 | } |
---|
| 1088 | } else { |
---|
| 1089 | obuf = g_output_buffer; |
---|
| 1090 | olen = g_output_buffer_len; |
---|
| 1091 | } |
---|
| 1092 | |
---|
[585] | 1093 | errno = 0; |
---|
| 1094 | |
---|
| 1095 | if ( ss->codecfilter == -1 ) { |
---|
[1042] | 1096 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
| 1097 | if ( s->fh == -1 && roar_vio_get_fh(&(ss->vio)) == -1 ) |
---|
[1014] | 1098 | return 0; |
---|
| 1099 | |
---|
[1157] | 1100 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
[625] | 1101 | if ( need_to_free ) free(obuf); |
---|
[981] | 1102 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[585] | 1103 | return 0; |
---|
[625] | 1104 | } |
---|
[1157] | 1105 | |
---|
| 1106 | if ( ret > 0 && errno == 0 ) { |
---|
[1231] | 1107 | 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] | 1108 | if ( need_to_free ) free(obuf); |
---|
| 1109 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
| 1110 | return 0; |
---|
| 1111 | } |
---|
[585] | 1112 | } else { |
---|
[1012] | 1113 | errno = 0; |
---|
[625] | 1114 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
| 1115 | == olen ) { |
---|
| 1116 | if ( need_to_free ) free(obuf); |
---|
[981] | 1117 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[585] | 1118 | return 0; |
---|
| 1119 | } else { // we cann't retry on codec filetered streams |
---|
[1012] | 1120 | if ( errno != EAGAIN ) { |
---|
| 1121 | if ( need_to_free ) free(obuf); |
---|
| 1122 | streams_delete(id); |
---|
| 1123 | return -1; |
---|
| 1124 | } |
---|
[585] | 1125 | } |
---|
| 1126 | } |
---|
[0] | 1127 | |
---|
[129] | 1128 | if ( errno == EAGAIN ) { |
---|
| 1129 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
| 1130 | // make any gapes in any output because of this |
---|
| 1131 | |
---|
[1750] | 1132 | #ifdef ROAR_HAVE_USLEEP |
---|
[129] | 1133 | usleep(100); // 0.1ms |
---|
[1750] | 1134 | #endif |
---|
[129] | 1135 | |
---|
[881] | 1136 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
[625] | 1137 | if ( need_to_free ) free(obuf); |
---|
[981] | 1138 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[129] | 1139 | return 0; |
---|
[1015] | 1140 | } else if ( errno == EAGAIN ) { |
---|
| 1141 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
| 1142 | return 0; |
---|
[625] | 1143 | } |
---|
[129] | 1144 | } |
---|
| 1145 | |
---|
[0] | 1146 | // ug... error... delete stream! |
---|
| 1147 | |
---|
[625] | 1148 | if ( need_to_free ) free(obuf); |
---|
[0] | 1149 | streams_delete(id); |
---|
| 1150 | |
---|
| 1151 | return -1; |
---|
| 1152 | } |
---|
| 1153 | |
---|
| 1154 | int streams_send_filter(int id) { |
---|
| 1155 | int fh; |
---|
[127] | 1156 | int have = 0; |
---|
| 1157 | int len; |
---|
[0] | 1158 | struct roar_stream * s; |
---|
| 1159 | struct roar_stream_server * ss; |
---|
| 1160 | |
---|
| 1161 | if ( g_streams[id] == NULL ) |
---|
| 1162 | return -1; |
---|
| 1163 | |
---|
| 1164 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
| 1165 | |
---|
[609] | 1166 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 1167 | |
---|
| 1168 | if ( (fh = s->fh) == -1 ) |
---|
| 1169 | return 0; |
---|
| 1170 | |
---|
| 1171 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
| 1172 | return 0; |
---|
| 1173 | |
---|
[1585] | 1174 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 1175 | return 0; |
---|
| 1176 | |
---|
| 1177 | |
---|
[0] | 1178 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
| 1179 | |
---|
[881] | 1180 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
[127] | 1181 | while ( have < g_output_buffer_len ) { |
---|
[881] | 1182 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
[127] | 1183 | streams_delete(id); |
---|
| 1184 | return -1; |
---|
| 1185 | } |
---|
| 1186 | have += len; |
---|
[0] | 1187 | } |
---|
[127] | 1188 | return 0; |
---|
[0] | 1189 | } |
---|
| 1190 | |
---|
| 1191 | // ug... error... delete stream! |
---|
| 1192 | |
---|
| 1193 | streams_delete(id); |
---|
| 1194 | |
---|
| 1195 | return -1; |
---|
| 1196 | } |
---|
| 1197 | |
---|
[596] | 1198 | |
---|
| 1199 | // VIO: |
---|
| 1200 | |
---|
| 1201 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
| 1202 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1203 | |
---|
| 1204 | if ( !s ) |
---|
| 1205 | return -1; |
---|
| 1206 | |
---|
| 1207 | return stream_vio_s_read(s, buf, count); |
---|
| 1208 | } |
---|
| 1209 | |
---|
| 1210 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
| 1211 | struct roar_stream_server * s = g_streams[stream]; |
---|
| 1212 | |
---|
| 1213 | if ( !s ) |
---|
| 1214 | return -1; |
---|
| 1215 | |
---|
| 1216 | return stream_vio_s_write(s, buf, count); |
---|
| 1217 | } |
---|
| 1218 | |
---|
| 1219 | |
---|
| 1220 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[741] | 1221 | size_t len = 0; |
---|
| 1222 | ssize_t r = -1; |
---|
[739] | 1223 | |
---|
[596] | 1224 | errno = 0; |
---|
| 1225 | |
---|
| 1226 | if ( !stream ) |
---|
| 1227 | return -1; |
---|
| 1228 | |
---|
[1613] | 1229 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[881] | 1230 | |
---|
[596] | 1231 | if ( ! stream->vio.read ) |
---|
| 1232 | return -1; |
---|
| 1233 | |
---|
[881] | 1234 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
[739] | 1235 | len += r; |
---|
| 1236 | buf += r; |
---|
| 1237 | count -= r; |
---|
| 1238 | if ( count == 0 ) |
---|
| 1239 | break; |
---|
| 1240 | } |
---|
| 1241 | |
---|
[740] | 1242 | if ( len == 0 && r == -1 ) |
---|
| 1243 | return -1; |
---|
| 1244 | |
---|
[739] | 1245 | return len; |
---|
[596] | 1246 | } |
---|
| 1247 | |
---|
| 1248 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
| 1249 | errno = 0; |
---|
| 1250 | |
---|
| 1251 | if ( !stream ) |
---|
| 1252 | return -1; |
---|
| 1253 | |
---|
[1613] | 1254 | /* |
---|
[934] | 1255 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
| 1256 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[1613] | 1257 | */ |
---|
[934] | 1258 | |
---|
| 1259 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
[596] | 1260 | |
---|
[881] | 1261 | return roar_vio_write(&(stream->vio), buf, count); |
---|
[596] | 1262 | } |
---|
| 1263 | |
---|
[0] | 1264 | //ll |
---|