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