[0] | 1 | //streams.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[668] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[668] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
[0] | 26 | #include "roard.h" |
---|
| 27 | |
---|
[5192] | 28 | // declared 'extern' |
---|
| 29 | struct roar_stream_server * g_streams[ROAR_STREAMS_MAX]; |
---|
| 30 | |
---|
[2734] | 31 | #define _CHECK_SID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_STREAMS_MAX || g_streams[(id)] == NULL ) return (ret) |
---|
| 32 | #define _CHECK_SID(id) _CHECK_SID_RET((id), -1) |
---|
| 33 | |
---|
[4967] | 34 | static int streams_thru_num = 0; |
---|
| 35 | static int streams_sync_num = 0; |
---|
[2417] | 36 | int streams_recsource_id = -1; |
---|
[2254] | 37 | |
---|
[4934] | 38 | static const struct rolestack * streams_rolestack = NULL; |
---|
| 39 | static int streams_role_num = 0; |
---|
| 40 | |
---|
| 41 | |
---|
[4336] | 42 | static void _streams_change_state(struct roar_stream_server * s, const int new, const char * func) { |
---|
| 43 | register int id = ROAR_STREAM(s)->id; |
---|
| 44 | register int old = s->state; |
---|
| 45 | |
---|
| 46 | s->state = new; |
---|
| 47 | |
---|
| 48 | if ( func == NULL ) { |
---|
| 49 | func = "(unknown)"; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | ROAR_INFO("_streams_change_state[by %s](id=%i): stream state: %s->%s", ROAR_DBG_INFO_VERBOSE, |
---|
| 53 | func, |
---|
| 54 | ROAR_STREAM(s)->id, |
---|
| 55 | roar_streamstate2str(old), roar_streamstate2str(new)); |
---|
| 56 | |
---|
| 57 | roar_notify_core_emit_simple(ROAR_OE_BASICS_CHANGE_STATE, -1, id, ROAR_OT_STREAM, old, new, NULL, 0); |
---|
| 58 | } |
---|
| 59 | |
---|
[4846] | 60 | void streams_change_sync_num(int id, int diff) { |
---|
| 61 | struct roar_stream_server * ss; |
---|
| 62 | int i; |
---|
| 63 | |
---|
| 64 | if ( diff != 0 ) |
---|
| 65 | if ( streams_get_dir(id) != ROAR_DIR_OUTPUT ) |
---|
| 66 | return; |
---|
| 67 | |
---|
| 68 | ROAR_INFO("streams_change_sync_num(id=%i, diff=%i): Number of sync streams changed from %i to %i", |
---|
| 69 | ROAR_DBG_INFO_VERBOSE, |
---|
| 70 | id, diff, |
---|
| 71 | streams_sync_num, streams_sync_num + diff); |
---|
| 72 | streams_sync_num += diff; |
---|
| 73 | |
---|
| 74 | if ( !streams_sync_num && alive ) { |
---|
| 75 | ROAR_INFO("streams_change_sync_num(id=%i, diff=%i): Number of sync streams changed to zero. Bad.", |
---|
| 76 | ROAR_DBG_INFO_VERBOSE, id, diff |
---|
| 77 | ); |
---|
| 78 | |
---|
| 79 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 80 | if ( (ss = g_streams[i]) == NULL ) |
---|
| 81 | continue; |
---|
| 82 | |
---|
| 83 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_OUTPUT ) |
---|
| 84 | continue; |
---|
| 85 | |
---|
| 86 | if ( !ss->ready ) |
---|
| 87 | continue; |
---|
| 88 | |
---|
| 89 | if ( ss->state == ROAR_STREAMSTATE_CLOSING ) |
---|
| 90 | continue; |
---|
| 91 | |
---|
| 92 | if ( streams_get_flag(i, ROAR_FLAG_PRIMARY) ) { |
---|
| 93 | ROAR_DBG("streams_change_sync_num(*): try to set sync on primary stream %i", i); |
---|
| 94 | streams_set_flag(i, ROAR_FLAG_SYNC); |
---|
| 95 | if ( streams_sync_num ) |
---|
| 96 | return; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 101 | if ( (ss = g_streams[i]) == NULL ) |
---|
| 102 | continue; |
---|
| 103 | |
---|
| 104 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_OUTPUT ) |
---|
| 105 | continue; |
---|
| 106 | |
---|
| 107 | if ( !ss->ready ) |
---|
| 108 | continue; |
---|
| 109 | |
---|
| 110 | if ( ss->state == ROAR_STREAMSTATE_CLOSING ) |
---|
| 111 | continue; |
---|
| 112 | |
---|
| 113 | if ( !streams_get_flag(i, ROAR_FLAG_PRIMARY) ) { |
---|
| 114 | ROAR_DBG("streams_change_sync_num(*): try to set sync on non-primary stream %i", i); |
---|
| 115 | streams_set_flag(i, ROAR_FLAG_SYNC); |
---|
| 116 | if ( streams_sync_num ) |
---|
| 117 | return; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | ROAR_INFO("streams_change_sync_num(id=%i, diff=%i): Can not auto set sync flag to an output. Very Bad", |
---|
| 122 | ROAR_DBG_INFO_VERBOSE, id, diff |
---|
| 123 | ); |
---|
| 124 | ROAR_WARN("streams_change_sync_num(id=%i, diff=%i): Lost all sync streams and failed to set a new one. Very Bad.", |
---|
| 125 | id, diff); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
[0] | 129 | int streams_init (void) { |
---|
| 130 | int i; |
---|
| 131 | |
---|
[4934] | 132 | streams_rolestack = rolestack_get_role(-1); // get default |
---|
| 133 | |
---|
[0] | 134 | for (i = 0; i < ROAR_STREAMS_MAX; i++) |
---|
| 135 | g_streams[i] = NULL; |
---|
| 136 | |
---|
| 137 | return 0; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | int streams_free (void) { |
---|
| 141 | int i; |
---|
| 142 | |
---|
| 143 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 144 | if ( g_streams[i] != NULL ) { |
---|
| 145 | streams_delete(i); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return 0; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | int streams_new (void) { |
---|
[16] | 154 | int i, j; |
---|
[494] | 155 | struct roar_stream * n = NULL; |
---|
| 156 | struct roar_stream_server * s = NULL; |
---|
[0] | 157 | |
---|
[1498] | 158 | #ifdef ROAR_SUPPORT_LISTEN |
---|
[1155] | 159 | if ( g_terminate && !g_no_listen ) // don't accept new streams in case of termination state |
---|
[1148] | 160 | return -1; |
---|
[1498] | 161 | #else |
---|
| 162 | if ( g_terminate ) // don't accept new streams in case of termination state |
---|
| 163 | return -1; |
---|
| 164 | #endif |
---|
[1148] | 165 | |
---|
[0] | 166 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 167 | if ( g_streams[i] == NULL ) { |
---|
[3063] | 168 | s = ROAR_STREAM_SERVER(n = ROAR_STREAM(roar_mm_malloc(sizeof(struct roar_stream_server)))); |
---|
[0] | 169 | if ( n == NULL ) { |
---|
| 170 | ROAR_ERR("streams_new(void): can not allocate memory for new stream: %s", strerror(errno)); |
---|
| 171 | ROAR_DBG("streams_new(void) = -1"); |
---|
| 172 | return -1; |
---|
| 173 | } |
---|
| 174 | |
---|
[3671] | 175 | memset(s, 0, sizeof(struct roar_stream_server)); |
---|
| 176 | |
---|
[0] | 177 | n->id = i; |
---|
| 178 | n->fh = -1; |
---|
[2714] | 179 | n->pos_rel_id = -1; |
---|
[1804] | 180 | /* |
---|
[0] | 181 | n->database = NULL; |
---|
| 182 | n->dataoff = NULL; |
---|
| 183 | n->datalen = 0; |
---|
| 184 | n->offset = 0; |
---|
[1804] | 185 | */ |
---|
[381] | 186 | n->pos = 0; |
---|
[0] | 187 | |
---|
[1842] | 188 | s->name = NULL; |
---|
| 189 | |
---|
[2611] | 190 | s->state = ROAR_STREAMSTATE_INITING; |
---|
| 191 | |
---|
[494] | 192 | s->client = -1; |
---|
| 193 | s->socktype = ROAR_SOCKET_TYPE_UNKNOWN; |
---|
| 194 | s->buffer = NULL; |
---|
| 195 | s->need_extra = 0; |
---|
| 196 | s->output = NULL; |
---|
| 197 | s->is_new = 1; |
---|
| 198 | s->codecfilter = -1; |
---|
[538] | 199 | s->pre_underruns = 0; |
---|
| 200 | s->post_underruns = 0; |
---|
[1137] | 201 | s->delay = 0; |
---|
[538] | 202 | s->codec_orgi = -1; |
---|
[643] | 203 | s->primary = 0; |
---|
[1913] | 204 | s->ready = 0; |
---|
[2151] | 205 | s->outputbuffer = NULL; |
---|
[2816] | 206 | s->prethru = NULL; |
---|
[3213] | 207 | s->mixer_stream = -1; |
---|
[3630] | 208 | s->role = ROAR_ROLE_UNKNOWN; |
---|
[4099] | 209 | s->parent_stream = -1; |
---|
[4541] | 210 | s->single_sink_c = 0; |
---|
[4553] | 211 | s->single_sink_self_c = 0; |
---|
[494] | 212 | |
---|
| 213 | s->mixer.scale = 65535; |
---|
| 214 | s->mixer.rpg_mul = 1; |
---|
| 215 | s->mixer.rpg_div = 1; |
---|
[16] | 216 | for (j = 0; j < ROAR_MAX_CHANNELS; j++) |
---|
[494] | 217 | s->mixer.mixer[j] = 65535; |
---|
| 218 | |
---|
[1498] | 219 | #ifdef ROAR_SUPPORT_META |
---|
[90] | 220 | for (j = 0; j < ROAR_META_MAX_PER_STREAM; j++) { |
---|
[494] | 221 | s->meta[j].type = ROAR_META_TYPE_NONE; |
---|
| 222 | s->meta[j].key[0] = 0; |
---|
| 223 | s->meta[j].value = NULL; |
---|
[90] | 224 | } |
---|
[1498] | 225 | #endif |
---|
[0] | 226 | |
---|
[5256] | 227 | roar_vio_clear_calls(&(s->vio)); |
---|
[4964] | 228 | roar_vio_clear_calls(&(s->jumbo)); |
---|
[3042] | 229 | s->viop = &(s->vio); |
---|
[930] | 230 | s->driver_id = -1; |
---|
[1030] | 231 | s->flags = ROAR_FLAG_NONE; |
---|
[592] | 232 | |
---|
[4753] | 233 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
[4361] | 234 | s->mixerstream = NULL; |
---|
[4753] | 235 | #endif |
---|
[4361] | 236 | |
---|
[1857] | 237 | //roardsp_fchain_init(&(s->fc)); |
---|
[981] | 238 | |
---|
[494] | 239 | g_streams[i] = s; |
---|
[4101] | 240 | counters_inc(streams, 1); |
---|
[4237] | 241 | ROAR_INFO("streams_new(void): New stream %i created (state=initing)", ROAR_DBG_INFO_VERBOSE, i); |
---|
[491] | 242 | ROAR_DBG("streams_new(void): n->id=%i", n->id); |
---|
[4346] | 243 | roar_notify_core_emit_snoargs(ROAR_OE_BASICS_NEW, -1, i, ROAR_OT_STREAM); |
---|
[0] | 244 | ROAR_DBG("streams_new(void) = %i", i); |
---|
| 245 | return i; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | return -1; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | int streams_delete (int id) { |
---|
[643] | 253 | struct roar_stream_server * s; |
---|
[645] | 254 | int prim; |
---|
[1245] | 255 | int no_vio_close = 0; |
---|
[1836] | 256 | int i; |
---|
[2609] | 257 | int client; |
---|
[645] | 258 | |
---|
[2734] | 259 | _CHECK_SID(id); |
---|
| 260 | |
---|
[643] | 261 | if ( (s = g_streams[id]) == NULL ) |
---|
[0] | 262 | return 0; |
---|
| 263 | |
---|
| 264 | ROAR_DBG("streams_delete(id=%i) = ?", id); |
---|
[643] | 265 | ROAR_DBG("streams_delete(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(s)->id); |
---|
[0] | 266 | |
---|
[2611] | 267 | // in case we are allready closing it... |
---|
| 268 | if ( s->state == ROAR_STREAMSTATE_CLOSING ) |
---|
| 269 | return 0; |
---|
| 270 | |
---|
[4336] | 271 | _streams_change_state(s, ROAR_STREAMSTATE_CLOSING, "streams_delete"); |
---|
[4346] | 272 | roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_STREAM); |
---|
[2611] | 273 | |
---|
[4934] | 274 | streams_set_role(id, -1); |
---|
| 275 | |
---|
[4847] | 276 | prim = s->primary; |
---|
| 277 | |
---|
| 278 | if ( prim ) |
---|
| 279 | alive = 0; |
---|
| 280 | |
---|
[4544] | 281 | if ( s->flags & ROAR_FLAG_SINGLESINK ) |
---|
| 282 | streams_set_single_sink(id, 1); |
---|
| 283 | |
---|
[4101] | 284 | counters_inc(streams, -1); |
---|
| 285 | |
---|
[2417] | 286 | if ( streams_get_flag(id, ROAR_FLAG_RECSOURCE) == 1 ) |
---|
| 287 | streams_reset_flag(id, ROAR_FLAG_RECSOURCE); |
---|
| 288 | |
---|
[1836] | 289 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 290 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) { |
---|
[2239] | 291 | switch (ROAR_STREAM(g_streams[i])->dir) { |
---|
| 292 | case ROAR_DIR_THRU: |
---|
| 293 | case ROAR_DIR_RAW_IN: |
---|
[2592] | 294 | if ( i != id ) |
---|
| 295 | streams_delete(i); |
---|
[2239] | 296 | break; |
---|
| 297 | default: |
---|
[2592] | 298 | if ( streams_get_flag(i, ROAR_FLAG_VIRTUAL) == 1 ) { |
---|
[2609] | 299 | if ( i != id ) { |
---|
| 300 | ROAR_DBG("streams_delete(id=%i): Deleting virtual child stream %i", id, i); |
---|
[2592] | 301 | streams_delete(i); |
---|
[2609] | 302 | } |
---|
[2592] | 303 | } else { |
---|
| 304 | ROAR_STREAM(g_streams[i])->pos_rel_id = -1; |
---|
| 305 | } |
---|
[1836] | 306 | } |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
[2255] | 310 | if ( ROAR_STREAM(s)->dir == ROAR_DIR_THRU ) |
---|
| 311 | streams_thru_num--; |
---|
| 312 | |
---|
[2603] | 313 | if ( streams_get_flag(id, ROAR_FLAG_VIRTUAL) == 1 ) { |
---|
[2605] | 314 | // we un-group the stream here to avoid a client deleting the parent deleting the client deleting ... |
---|
[2609] | 315 | i = ROAR_STREAM(s)->pos_rel_id; |
---|
| 316 | if ( i != -1 ) { |
---|
| 317 | ROAR_STREAM(s)->pos_rel_id = -1; |
---|
| 318 | client = streams_get_client(id); |
---|
| 319 | streams_set_client(id, -1); |
---|
| 320 | ROAR_DBG("streams_delete(id=%i): Stream has flag virtual, notifying parent stream %i", id, i); |
---|
[2612] | 321 | streams_ctl(i, ROAR_CODECFILTER_CTL_VIRTUAL_DELETE|ROAR_STREAM_CTL_TYPE_INT, &id); |
---|
[2609] | 322 | ROAR_DBG("streams_delete(id=%i): Notify send to stream %i", id, i); |
---|
| 323 | streams_set_client(id, client); |
---|
| 324 | ROAR_STREAM(s)->pos_rel_id = i; |
---|
| 325 | } |
---|
[2603] | 326 | } |
---|
| 327 | |
---|
[1498] | 328 | #ifdef ROAR_SUPPORT_META |
---|
[1045] | 329 | // delete meta data form other meta streams if needed |
---|
| 330 | if ( streams_get_flag(id, ROAR_FLAG_META) == 1 ) { |
---|
| 331 | ROAR_DBG("streams_delete(id=%i): deleting meta stream!", id); |
---|
| 332 | stream_meta_clear(id); |
---|
| 333 | stream_meta_finalize(id); |
---|
| 334 | } |
---|
[1498] | 335 | #endif |
---|
[1045] | 336 | |
---|
[643] | 337 | if ( s->codecfilter != -1 ) { |
---|
| 338 | codecfilter_close(s->codecfilter_inst, s->codecfilter); |
---|
| 339 | s->codecfilter_inst = NULL; |
---|
| 340 | s->codecfilter = -1; |
---|
| 341 | } |
---|
| 342 | |
---|
[936] | 343 | if ( s->driver_id != -1 ) { |
---|
[937] | 344 | driver_closevio(&(s->vio), s->driver_id); |
---|
[4964] | 345 | roar_vio_clear_calls(&(s->vio)); |
---|
[936] | 346 | s->driver_id = -1; |
---|
[1245] | 347 | no_vio_close = 1; |
---|
[936] | 348 | } |
---|
| 349 | |
---|
[4753] | 350 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
[4361] | 351 | if ( s->mixerstream != NULL ) { |
---|
| 352 | hwmixer_close(id); |
---|
| 353 | } |
---|
[4753] | 354 | #endif |
---|
[4361] | 355 | |
---|
[4847] | 356 | // all IO is no disconnected: |
---|
| 357 | s->ready = 0; |
---|
| 358 | |
---|
| 359 | // re-try reset after all IO is disconnected. |
---|
| 360 | if ( streams_get_flag(id, ROAR_FLAG_RECSOURCE) == 1 ) |
---|
| 361 | streams_reset_flag(id, ROAR_FLAG_RECSOURCE); |
---|
| 362 | |
---|
[1857] | 363 | //roardsp_fchain_uninit(&(s->fc)); |
---|
[981] | 364 | |
---|
[643] | 365 | if ( s->client != -1 ) { |
---|
| 366 | ROAR_DBG("streams_delete(id=%i): Stream is owned by client %i", id, g_streams[id]->client); |
---|
| 367 | client_stream_delete(s->client, id); |
---|
[269] | 368 | } |
---|
| 369 | |
---|
[2151] | 370 | stream_outputbuffer_destroy(id); |
---|
[2816] | 371 | stream_prethru_destroy(id); |
---|
[2151] | 372 | |
---|
[643] | 373 | if ( s->buffer != NULL ) |
---|
| 374 | roar_buffer_free(s->buffer); |
---|
| 375 | |
---|
| 376 | if ( s->output != NULL ) |
---|
[4957] | 377 | roar_mm_free(s->output); |
---|
[643] | 378 | |
---|
[1243] | 379 | /* |
---|
[643] | 380 | if ( ROAR_STREAM(s)->fh != -1 ) |
---|
| 381 | close(ROAR_STREAM(s)->fh); |
---|
[1243] | 382 | */ |
---|
| 383 | |
---|
[4846] | 384 | if ( s->flags & ROAR_FLAG_SYNC ) |
---|
| 385 | streams_change_sync_num(id, -1); |
---|
| 386 | |
---|
[1245] | 387 | if ( !no_vio_close ) |
---|
[3042] | 388 | roar_vio_close(s->viop); |
---|
[643] | 389 | |
---|
[1842] | 390 | if ( s->name != NULL ) |
---|
[4957] | 391 | roar_mm_free(s->name); |
---|
[1842] | 392 | |
---|
[3063] | 393 | roar_mm_free(s); |
---|
[0] | 394 | |
---|
| 395 | g_streams[id] = NULL; |
---|
| 396 | |
---|
[4102] | 397 | if ( prim && alive ) { |
---|
[645] | 398 | alive = 0; |
---|
| 399 | clean_quit(); |
---|
| 400 | } |
---|
| 401 | |
---|
[4238] | 402 | ROAR_INFO("streams_delete(id=%i): stream deleted", ROAR_DBG_INFO_VERBOSE, id); |
---|
[0] | 403 | ROAR_DBG("streams_delete(id=%i) = 0", id); |
---|
| 404 | return 0; |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | int streams_set_client (int id, int client) { |
---|
[2734] | 408 | |
---|
| 409 | _CHECK_SID(id); |
---|
[0] | 410 | |
---|
[491] | 411 | ROAR_DBG("streams_set_client(id=%i): g_streams[id]->id=%i", id, ROAR_STREAM(g_streams[id])->id); |
---|
[0] | 412 | g_streams[id]->client = client; |
---|
| 413 | |
---|
| 414 | return 0; |
---|
| 415 | } |
---|
| 416 | |
---|
[766] | 417 | int streams_get_client (int id) { |
---|
[2734] | 418 | _CHECK_SID(id); |
---|
[766] | 419 | |
---|
| 420 | return g_streams[id]->client; |
---|
| 421 | } |
---|
| 422 | |
---|
[1609] | 423 | int streams_set_dir (int id, int dir, int defaults) { |
---|
| 424 | struct roar_stream_server * ss; |
---|
[4543] | 425 | int mixer; |
---|
[1609] | 426 | |
---|
[2734] | 427 | _CHECK_SID(id); |
---|
| 428 | |
---|
[1609] | 429 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 430 | return -1; |
---|
| 431 | |
---|
| 432 | ROAR_STREAM(ss)->dir = dir; |
---|
| 433 | |
---|
[2255] | 434 | if ( dir == ROAR_DIR_THRU ) |
---|
| 435 | streams_thru_num++; |
---|
| 436 | |
---|
[1609] | 437 | if ( defaults ) { |
---|
| 438 | if ( dir <= 0 || dir >= ROAR_DIR_DIRIDS ) |
---|
| 439 | return -1; |
---|
| 440 | |
---|
[2387] | 441 | ROAR_DBG("streams_set_dir(*): g_config->streams[dir=%i].flags = 0x%.4x", dir, g_config->streams[dir].flags); |
---|
[1906] | 442 | |
---|
| 443 | if ( streams_set_flag(id, g_config->streams[dir].flags) == -1 ) { |
---|
| 444 | ROAR_DBG("streams_set_dir(*) = -1 // can not set stream flags"); |
---|
[1609] | 445 | return -1; |
---|
[1906] | 446 | } |
---|
[1609] | 447 | |
---|
| 448 | ss->mixer.scale = g_config->streams[dir].mixer.scale; |
---|
| 449 | ss->mixer.rpg_mul = g_config->streams[dir].mixer.rpg_mul; |
---|
| 450 | ss->mixer.rpg_div = g_config->streams[dir].mixer.rpg_div; |
---|
| 451 | } |
---|
| 452 | |
---|
[3214] | 453 | if ( dir != ROAR_DIR_MIXING ) { |
---|
| 454 | switch (streams_get_subsys(id)) { |
---|
| 455 | case ROAR_SUBSYS_WAVEFORM: |
---|
| 456 | streams_set_mixer_stream(id, g_waveform_mixer.stream); |
---|
[3539] | 457 | roardsp_chanlist_init(ss->chanmap.in, ROAR_STREAM(ss)->info.channels, ROARDSP_CHANLIST_MAP_ROARAUDIO); |
---|
[3214] | 458 | break; |
---|
| 459 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
| 460 | case ROAR_SUBSYS_MIDI: |
---|
[3544] | 461 | roardsp_chanlist_init(ss->chanmap.in, ROAR_STREAM(ss)->info.channels, ROARDSP_CHANLIST_MAP_MIDI); |
---|
[3214] | 462 | streams_set_mixer_stream(id, g_midi_mixer.stream); |
---|
| 463 | break; |
---|
| 464 | #endif |
---|
| 465 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
| 466 | case ROAR_SUBSYS_LIGHT: |
---|
| 467 | streams_set_mixer_stream(id, g_light_mixer.stream); |
---|
| 468 | break; |
---|
| 469 | #endif |
---|
| 470 | } |
---|
[3544] | 471 | |
---|
[4545] | 472 | if ( streams_get_ssdir(id) & STREAM_DIR_OUT ) { |
---|
| 473 | if ( (mixer = streams_get_mixer_stream(id)) != -1 ) { |
---|
| 474 | if ( g_streams[mixer]->flags & ROAR_FLAG_SINGLESINK ) { |
---|
| 475 | streams_set_mixer_stream(id, -1); |
---|
| 476 | return -1; |
---|
| 477 | } |
---|
[4543] | 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
[3544] | 481 | memcpy(ss->chanmap.out, ss->chanmap.in, sizeof(ss->chanmap.out)); |
---|
[3546] | 482 | streams_set_map(id, NULL, 0); |
---|
[3214] | 483 | } else { |
---|
| 484 | streams_set_mixer_stream(id, id); |
---|
| 485 | } |
---|
| 486 | |
---|
[1906] | 487 | ROAR_DBG("streams_set_dir(*) = 0"); |
---|
[1609] | 488 | return 0; |
---|
| 489 | } |
---|
[766] | 490 | |
---|
[2251] | 491 | int streams_get_dir (int id) { |
---|
| 492 | struct roar_stream_server * ss; |
---|
| 493 | |
---|
[2734] | 494 | _CHECK_SID(id); |
---|
| 495 | |
---|
[2251] | 496 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 497 | return -1; |
---|
| 498 | |
---|
| 499 | return ROAR_STREAM(ss)->dir; |
---|
| 500 | } |
---|
| 501 | |
---|
[3213] | 502 | int streams_set_mixer_stream(int id, int mixer) { |
---|
| 503 | struct roar_stream_server * ss; |
---|
| 504 | |
---|
| 505 | _CHECK_SID(id); |
---|
| 506 | |
---|
| 507 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 508 | return -1; |
---|
| 509 | |
---|
| 510 | ss->mixer_stream = mixer; |
---|
| 511 | |
---|
| 512 | return 0; |
---|
| 513 | } |
---|
| 514 | |
---|
[4543] | 515 | int streams_get_mixer_stream(int id) { |
---|
[3213] | 516 | struct roar_stream_server * ss; |
---|
| 517 | |
---|
| 518 | _CHECK_SID(id); |
---|
| 519 | |
---|
| 520 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 521 | return -1; |
---|
| 522 | |
---|
| 523 | return ss->mixer_stream; |
---|
| 524 | } |
---|
| 525 | |
---|
[4934] | 526 | static int streams_set_role_handle(int id) { |
---|
| 527 | switch (streams_rolestack->action) { |
---|
| 528 | case RS_ERROR: /* nothing to do */; break; |
---|
| 529 | case RS_MIX: /* nothing to do */; break; |
---|
| 530 | case RS_KICK: streams_delete(id); break; |
---|
| 531 | case RS_MUTE: streams_set_flag(id, ROAR_FLAG_MUTE); break; |
---|
| 532 | case RS_PAUSE: streams_set_flag(id, ROAR_FLAG_PAUSE); break; |
---|
| 533 | } |
---|
| 534 | return 0; |
---|
| 535 | } |
---|
| 536 | |
---|
[3630] | 537 | int streams_set_role (int id, int role) { |
---|
| 538 | struct roar_stream_server * ss; |
---|
[4934] | 539 | const struct rolestack * rs; |
---|
| 540 | int old_role; |
---|
| 541 | int i; |
---|
| 542 | |
---|
| 543 | ROAR_DBG("streams_set_role(id=%i, role=%i) = ?", id, role); |
---|
[3630] | 544 | |
---|
| 545 | _CHECK_SID(id); |
---|
| 546 | |
---|
| 547 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 548 | return -1; |
---|
| 549 | |
---|
[4934] | 550 | old_role = ss->role; |
---|
[3630] | 551 | ss->role = role; |
---|
| 552 | |
---|
[4934] | 553 | ROAR_DBG("streams_set_role(id=%i, role=%i) = ?", id, role); |
---|
| 554 | |
---|
| 555 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_PLAY ) |
---|
| 556 | return 0; |
---|
| 557 | |
---|
| 558 | ROAR_DBG("streams_set_role(id=%i, role=%i) = ?", id, role); |
---|
| 559 | |
---|
| 560 | if ( old_role == role ) |
---|
| 561 | return 0; |
---|
| 562 | |
---|
| 563 | if ( old_role != role && old_role != -1 && role != -1 ) { |
---|
| 564 | streams_set_role(id, -1); |
---|
| 565 | streams_set_role(id, role); |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | ROAR_DBG("streams_set_role(id=%i, role=%i) = ?", id, role); |
---|
| 569 | |
---|
| 570 | if ( ss->role != -1 && ss->role == streams_rolestack->role ) { |
---|
| 571 | ROAR_DBG("streams_set_role(id=%i, role=%i) = ?", id, role); |
---|
| 572 | |
---|
| 573 | streams_role_num--; |
---|
| 574 | if ( streams_role_num == 0 ) { |
---|
| 575 | streams_rolestack = rolestack_get_role(-1); // get default |
---|
| 576 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 577 | if ( (ss = g_streams[i]) == NULL ) |
---|
| 578 | continue; |
---|
| 579 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_PLAY ) |
---|
| 580 | continue; |
---|
| 581 | if ( ss->role == -1 ) |
---|
| 582 | continue; |
---|
| 583 | rs = rolestack_get_role(ss->role); |
---|
| 584 | if ( rs->index > streams_rolestack->role ) { |
---|
| 585 | streams_rolestack = rs; |
---|
| 586 | } |
---|
| 587 | } |
---|
| 588 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 589 | if ( (ss = g_streams[i]) == NULL ) |
---|
| 590 | continue; |
---|
| 591 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_PLAY ) |
---|
| 592 | continue; |
---|
| 593 | if ( ss->role == streams_rolestack->role ) |
---|
| 594 | streams_role_num++; |
---|
| 595 | } |
---|
| 596 | } |
---|
| 597 | } else if ( role != -1 ) { |
---|
| 598 | ROAR_DBG("streams_set_role(id=%i, role=%i) = ?", id, role); |
---|
| 599 | |
---|
| 600 | rs = rolestack_get_role(role); |
---|
| 601 | ROAR_DBG("streams_set_role(id=%i, role=%i): rs->index=%i, streams_rolestack->index=%i", id, role, rs->index, streams_rolestack->index); |
---|
| 602 | if ( rs->index == streams_rolestack->index ) { |
---|
| 603 | ROAR_DBG("streams_set_role(id=%i, role=%i): rs->index=%i, streams_rolestack->index=%i", id, role, rs->index, streams_rolestack->index); |
---|
| 604 | streams_role_num++; |
---|
| 605 | } else if ( rs->index > streams_rolestack->index ) { |
---|
| 606 | ROAR_DBG("streams_set_role(id=%i, role=%i): rs=%p{.index=%i, .action=%s}", id, role, rs, rs->index, rolestack_action2str(rs->action)); |
---|
| 607 | streams_role_num = 1; |
---|
| 608 | streams_rolestack = rs; |
---|
| 609 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 610 | if ( (ss = g_streams[i]) == NULL ) |
---|
| 611 | continue; |
---|
| 612 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_PLAY ) |
---|
| 613 | continue; |
---|
| 614 | rs = rolestack_get_role(ss->role); |
---|
| 615 | ROAR_DBG("streams_set_role(id=%i, role=%i): i=%i, rs->index=%i, streams_rolestack->index=%i", id, role, i, rs->index, streams_rolestack->index); |
---|
| 616 | if ( rs->index < streams_rolestack->index ) |
---|
| 617 | streams_set_role_handle(i); |
---|
| 618 | } |
---|
| 619 | } else { |
---|
| 620 | ROAR_DBG("streams_set_role(id=%i, role=%i): streams_rolestack->action=%s", id, role, rolestack_action2str(streams_rolestack->action)); |
---|
| 621 | streams_set_role_handle(id); |
---|
| 622 | } |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | ROAR_DBG("streams_set_role(id=%i, role=%i) = 0", id, role); |
---|
[3630] | 626 | return 0; |
---|
| 627 | } |
---|
| 628 | |
---|
[2415] | 629 | int streams_get_subsys (int id) { |
---|
| 630 | struct roar_stream_server * ss; |
---|
| 631 | |
---|
[2734] | 632 | _CHECK_SID(id); |
---|
| 633 | |
---|
[2415] | 634 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 635 | return -1; |
---|
| 636 | |
---|
[4614] | 637 | if ( ROAR_STREAM(ss)->dir == ROAR_DIR_THRU ) |
---|
| 638 | return streams_get_subsys(ROAR_STREAM(ss)->pos_rel_id); |
---|
| 639 | |
---|
| 640 | return streams_dir2subsys(ROAR_STREAM(ss)->dir); |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | int streams_dir2subsys (int dir) { |
---|
| 644 | switch (dir) { |
---|
[2415] | 645 | case ROAR_DIR_PLAY: |
---|
| 646 | case ROAR_DIR_RECORD: |
---|
| 647 | case ROAR_DIR_MONITOR: |
---|
| 648 | case ROAR_DIR_FILTER: |
---|
| 649 | case ROAR_DIR_OUTPUT: |
---|
| 650 | case ROAR_DIR_BIDIR: |
---|
| 651 | return ROAR_SUBSYS_WAVEFORM; |
---|
| 652 | break; |
---|
| 653 | case ROAR_DIR_MIDI_IN: |
---|
| 654 | case ROAR_DIR_MIDI_OUT: |
---|
| 655 | return ROAR_SUBSYS_MIDI; |
---|
| 656 | break; |
---|
| 657 | case ROAR_DIR_LIGHT_IN: |
---|
| 658 | case ROAR_DIR_LIGHT_OUT: |
---|
| 659 | return ROAR_SUBSYS_LIGHT; |
---|
| 660 | break; |
---|
| 661 | case ROAR_DIR_RAW_IN: |
---|
| 662 | case ROAR_DIR_RAW_OUT: |
---|
| 663 | return ROAR_SUBSYS_RAW; |
---|
| 664 | break; |
---|
[2681] | 665 | case ROAR_DIR_COMPLEX_IN: |
---|
| 666 | case ROAR_DIR_COMPLEX_OUT: |
---|
| 667 | return ROAR_SUBSYS_COMPLEX; |
---|
| 668 | break; |
---|
[4383] | 669 | case ROAR_DIR_MIXING: |
---|
| 670 | return ROAR_SUBSYS_NONE; |
---|
| 671 | break; |
---|
[2415] | 672 | } |
---|
| 673 | |
---|
| 674 | return -1; |
---|
| 675 | } |
---|
| 676 | |
---|
[3542] | 677 | int streams_get_ssdir (int id) { |
---|
| 678 | struct roar_stream_server * ss; |
---|
| 679 | |
---|
| 680 | _CHECK_SID(id); |
---|
| 681 | |
---|
| 682 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 683 | return -1; |
---|
| 684 | |
---|
| 685 | switch (ROAR_STREAM(ss)->dir) { |
---|
| 686 | case ROAR_DIR_PLAY: |
---|
| 687 | case ROAR_DIR_MIDI_IN: |
---|
| 688 | case ROAR_DIR_LIGHT_IN: |
---|
| 689 | case ROAR_DIR_RAW_IN: |
---|
| 690 | case ROAR_DIR_COMPLEX_IN: |
---|
| 691 | return STREAM_DIR_IN; |
---|
| 692 | break; |
---|
| 693 | case ROAR_DIR_RECORD: |
---|
| 694 | case ROAR_DIR_MONITOR: |
---|
| 695 | case ROAR_DIR_OUTPUT: |
---|
| 696 | case ROAR_DIR_MIDI_OUT: |
---|
| 697 | case ROAR_DIR_LIGHT_OUT: |
---|
| 698 | case ROAR_DIR_RAW_OUT: |
---|
| 699 | case ROAR_DIR_COMPLEX_OUT: |
---|
| 700 | return STREAM_DIR_OUT; |
---|
| 701 | break; |
---|
| 702 | case ROAR_DIR_MIXING: |
---|
| 703 | return STREAM_DIR_NONE; |
---|
| 704 | break; |
---|
| 705 | case ROAR_DIR_FILTER: |
---|
| 706 | case ROAR_DIR_BIDIR: |
---|
| 707 | return STREAM_DIR_BIDIR; |
---|
| 708 | break; |
---|
| 709 | case ROAR_DIR_THRU: |
---|
| 710 | return streams_get_ssdir(ROAR_STREAM(ss)->pos_rel_id); |
---|
| 711 | break; |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | return -1; |
---|
| 715 | } |
---|
| 716 | |
---|
[2593] | 717 | #define _err() streams_delete(id); return -1; |
---|
[2594] | 718 | int streams_new_virtual (int parent, struct roar_stream_server ** stream) { |
---|
[2593] | 719 | struct roar_stream_server * parent_ss, * ss; |
---|
| 720 | struct roar_stream * parent_s , * s; |
---|
| 721 | int id = -1; |
---|
| 722 | int client, dir; |
---|
| 723 | |
---|
| 724 | if ( streams_get(parent, &parent_ss) == -1 ) |
---|
| 725 | return -1; |
---|
| 726 | |
---|
| 727 | if ( (client = streams_get_client(parent)) == -1 ) |
---|
| 728 | return -1; |
---|
| 729 | |
---|
| 730 | if ( (dir = streams_get_dir(parent)) == -1 ) |
---|
| 731 | return -1; |
---|
| 732 | |
---|
| 733 | if ( (id = streams_new()) == -1 ) { |
---|
| 734 | return -1; |
---|
| 735 | } |
---|
| 736 | |
---|
[2597] | 737 | if ( client_stream_add(client, id) == -1 ) { |
---|
[2593] | 738 | _err(); |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | if ( streams_get(id, &ss) == -1 ) { |
---|
| 742 | _err(); |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | if ( streams_set_dir(id, dir, 1) == -1 ) { |
---|
| 746 | _err(); |
---|
| 747 | } |
---|
| 748 | |
---|
| 749 | s = ROAR_STREAM( ss); |
---|
| 750 | parent_s = ROAR_STREAM(parent_ss); |
---|
| 751 | |
---|
[2596] | 752 | s->pos_rel_id = parent; |
---|
[2593] | 753 | |
---|
[2625] | 754 | if ( streams_set_rawflag(id, ROAR_FLAG_VIRTUAL) == -1 ) { |
---|
[2593] | 755 | _err(); |
---|
| 756 | } |
---|
| 757 | |
---|
[2596] | 758 | if ( stream != NULL ) |
---|
[2594] | 759 | *stream = ss; |
---|
| 760 | |
---|
[2593] | 761 | return id; |
---|
| 762 | } |
---|
| 763 | #undef _err |
---|
| 764 | |
---|
[0] | 765 | int streams_set_fh (int id, int fh) { |
---|
[1243] | 766 | struct roar_stream_server * ss; |
---|
[1913] | 767 | struct roar_stream * s; |
---|
[51] | 768 | int dir; |
---|
[2361] | 769 | int nonblock = 1; |
---|
[51] | 770 | |
---|
[2734] | 771 | _CHECK_SID(id); |
---|
| 772 | |
---|
[1913] | 773 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[0] | 774 | return -1; |
---|
| 775 | |
---|
[4815] | 776 | if ( ss->ready && !(s->fh == -1 && fh == -2) ) { |
---|
[4240] | 777 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = -1 // try to set fh for stream which is already ready", id, fh); |
---|
[4229] | 778 | return -1; |
---|
[4240] | 779 | } |
---|
[4229] | 780 | |
---|
[4815] | 781 | dir = s->dir; |
---|
[2816] | 782 | |
---|
[4240] | 783 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): g_streams[id]->id=%i", id, fh, s->id); |
---|
[491] | 784 | |
---|
[1913] | 785 | s->fh = fh; |
---|
[0] | 786 | |
---|
[1243] | 787 | ROAR_DBG("streams_set_fh(id=%i, fh=%i): driverID=%i", id, fh, ss->driver_id); |
---|
| 788 | |
---|
[2766] | 789 | if ( ss->driver_id == -1 && fh != -2 ) { |
---|
| 790 | #ifndef ROAR_TARGET_WIN32 |
---|
[5256] | 791 | roar_vio_open_fh(&(ss->vio), fh); |
---|
[2766] | 792 | #else |
---|
| 793 | roar_vio_open_fh_socket(&(ss->vio), fh); |
---|
| 794 | #endif |
---|
| 795 | } |
---|
[1243] | 796 | |
---|
[2816] | 797 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 798 | |
---|
[2840] | 799 | switch (dir) { |
---|
| 800 | case ROAR_DIR_THRU: |
---|
| 801 | case ROAR_DIR_RAW_IN: |
---|
| 802 | case ROAR_DIR_RAW_OUT: |
---|
| 803 | break; |
---|
| 804 | default: |
---|
| 805 | if ( codecfilter_open(&(ss->codecfilter_inst), &(ss->codecfilter), NULL, |
---|
| 806 | s->info.codec, ss) == -1 ) { |
---|
| 807 | streams_delete(id); // TODO: FIXME: is this correct? shoudn't we return -1 in any case here? |
---|
| 808 | return -1; |
---|
| 809 | } |
---|
| 810 | break; |
---|
[571] | 811 | } |
---|
[268] | 812 | |
---|
[2816] | 813 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 814 | |
---|
[1610] | 815 | if ( fh == -2 ) { |
---|
| 816 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 817 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_READ_FH, &fh) == -1 ) { |
---|
| 818 | fh = -2; |
---|
| 819 | } else { |
---|
| 820 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 821 | if ( fh < 0 ) { |
---|
| 822 | fh = -2; |
---|
| 823 | } else { |
---|
[1913] | 824 | s->fh = fh; |
---|
[1610] | 825 | } |
---|
| 826 | } |
---|
| 827 | } |
---|
| 828 | |
---|
[2816] | 829 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
| 830 | |
---|
[1610] | 831 | if ( fh == -1 || fh == -2 ) { // yes, this is valid, indecats full vio! |
---|
[1913] | 832 | ss->ready = 1; |
---|
[4336] | 833 | //ROAR_INFO("streams_set_fh(id=%i, fh=%i): stream state: %s->new", ROAR_DBG_INFO_VERBOSE, id, fh, roar_streamstate2str(ss->state)); |
---|
| 834 | _streams_change_state(ss, ROAR_STREAMSTATE_NEW, "streams_set_fh"); |
---|
[634] | 835 | return 0; |
---|
| 836 | } |
---|
| 837 | |
---|
[2816] | 838 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = ?", id, fh); |
---|
[968] | 839 | |
---|
[2816] | 840 | // roar_socket_recvbuf(fh, ROAR_OUTPUT_CALC_OUTBUFSIZE( &(ROAR_STREAM(g_streams[id])->info) )); // set recv buffer to minimum |
---|
[51] | 841 | |
---|
[1912] | 842 | switch (dir) { |
---|
| 843 | case ROAR_DIR_MONITOR: |
---|
| 844 | case ROAR_DIR_RECORD: |
---|
| 845 | case ROAR_DIR_OUTPUT: |
---|
| 846 | case ROAR_DIR_MIDI_OUT: |
---|
| 847 | case ROAR_DIR_LIGHT_OUT: |
---|
[2248] | 848 | case ROAR_DIR_RAW_OUT: |
---|
[1912] | 849 | ROAR_SHUTDOWN(fh, SHUT_RD); |
---|
| 850 | break; |
---|
[51] | 851 | } |
---|
| 852 | |
---|
[2337] | 853 | if ( dir >= ROAR_DIR_DIRIDS ) |
---|
| 854 | return -1; |
---|
| 855 | |
---|
| 856 | if ( g_config->streams[dir].flags & ROAR_FLAG_SYNC ) { |
---|
[2361] | 857 | switch (dir) { |
---|
| 858 | case ROAR_DIR_BRIDGE: |
---|
| 859 | case ROAR_DIR_MIDI_OUT: |
---|
| 860 | break; |
---|
| 861 | default: |
---|
| 862 | nonblock = 0; |
---|
| 863 | break; |
---|
| 864 | } |
---|
| 865 | } |
---|
| 866 | |
---|
| 867 | |
---|
| 868 | if ( !nonblock ) { |
---|
[1913] | 869 | ss->ready = 1; |
---|
[4336] | 870 | //ROAR_INFO("streams_set_fh(id=%i, fh=%i): stream state: %s->new", ROAR_DBG_INFO_VERBOSE, id, fh, roar_streamstate2str(ss->state)); |
---|
| 871 | _streams_change_state(ss, ROAR_STREAMSTATE_NEW, "streams_set_fh"); |
---|
[2816] | 872 | |
---|
| 873 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = 0", id, fh); |
---|
[0] | 874 | return 0; |
---|
| 875 | } else { |
---|
[2764] | 876 | #ifndef ROAR_TARGET_WIN32 |
---|
[1913] | 877 | if ( roar_socket_nonblock(fh, ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 878 | return -1; |
---|
[2764] | 879 | #endif |
---|
[1913] | 880 | |
---|
| 881 | ss->ready = 1; |
---|
[4336] | 882 | //ROAR_INFO("streams_set_fh(id=%i, fh=%i): stream state: %s->new", ROAR_DBG_INFO_VERBOSE, id, fh, roar_streamstate2str(ss->state)); |
---|
| 883 | _streams_change_state(ss, ROAR_STREAMSTATE_NEW, "streams_set_fh"); |
---|
[2816] | 884 | |
---|
| 885 | ROAR_DBG("streams_set_fh(id=%i, fh=%i) = 0", id, fh); |
---|
[1913] | 886 | return 0; |
---|
[0] | 887 | } |
---|
| 888 | } |
---|
| 889 | |
---|
[66] | 890 | int streams_get_fh (int id) { |
---|
[2734] | 891 | _CHECK_SID(id); |
---|
[66] | 892 | |
---|
[609] | 893 | return ROAR_STREAM(g_streams[id])->fh; |
---|
[66] | 894 | } |
---|
[0] | 895 | |
---|
[2606] | 896 | int streams_set_null_io(int id) { |
---|
| 897 | struct roar_stream_server * ss; |
---|
| 898 | struct roar_stream * s; |
---|
| 899 | |
---|
[2734] | 900 | _CHECK_SID(id); |
---|
| 901 | |
---|
[2606] | 902 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
| 903 | return -1; |
---|
| 904 | |
---|
| 905 | s->fh = -1; |
---|
| 906 | |
---|
| 907 | return 0; |
---|
| 908 | } |
---|
| 909 | |
---|
[0] | 910 | int streams_get (int id, struct roar_stream_server ** stream) { |
---|
[2734] | 911 | _CHECK_SID(id); |
---|
[0] | 912 | |
---|
| 913 | *stream = g_streams[id]; |
---|
| 914 | |
---|
| 915 | return 0; |
---|
| 916 | } |
---|
| 917 | |
---|
[4521] | 918 | int streams_get_clientobj (int id, struct roar_stream ** stream) { |
---|
| 919 | _CHECK_SID(id); |
---|
| 920 | |
---|
| 921 | *stream = ROAR_STREAM(g_streams[id]); |
---|
| 922 | |
---|
| 923 | return 0; |
---|
| 924 | } |
---|
| 925 | |
---|
[377] | 926 | int streams_set_socktype (int id, int socktype) { |
---|
[2734] | 927 | _CHECK_SID(id); |
---|
[377] | 928 | |
---|
| 929 | g_streams[id]->socktype = socktype; |
---|
| 930 | |
---|
| 931 | return 0; |
---|
| 932 | } |
---|
| 933 | |
---|
| 934 | int streams_get_socktype (int id) { |
---|
[2734] | 935 | _CHECK_SID(id); |
---|
[377] | 936 | |
---|
| 937 | return g_streams[id]->socktype; |
---|
| 938 | } |
---|
[0] | 939 | |
---|
[3921] | 940 | int streams_is_ready (int id) { |
---|
| 941 | _CHECK_SID(id); |
---|
| 942 | |
---|
| 943 | return g_streams[id]->ready; |
---|
| 944 | } |
---|
| 945 | |
---|
[4819] | 946 | int streams_is_new (int id) { |
---|
| 947 | _CHECK_SID(id); |
---|
| 948 | |
---|
| 949 | return g_streams[id]->ready; |
---|
| 950 | } |
---|
| 951 | |
---|
[643] | 952 | int streams_set_primary (int id, int prim) { |
---|
[2734] | 953 | _CHECK_SID(id); |
---|
[643] | 954 | |
---|
| 955 | g_streams[id]->primary = prim; |
---|
| 956 | |
---|
| 957 | return 0; |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | int streams_mark_primary (int id) { |
---|
| 961 | return streams_set_primary(id, 1); |
---|
| 962 | } |
---|
[1029] | 963 | |
---|
[1116] | 964 | int streams_set_sync (int id, int sync) { |
---|
[1117] | 965 | int fh = streams_get_fh(id); |
---|
[1116] | 966 | |
---|
[4846] | 967 | ROAR_DBG("streams_set_sync(id=%i, sync=%i) = ?", id, sync); |
---|
| 968 | |
---|
[2734] | 969 | _CHECK_SID(id); |
---|
| 970 | |
---|
[4846] | 971 | if ( fh > -1 ) { |
---|
[1117] | 972 | if ( roar_socket_nonblock(fh, sync ? ROAR_SOCKET_BLOCK : ROAR_SOCKET_NONBLOCK) == -1 ) |
---|
| 973 | return -1; |
---|
[1116] | 974 | |
---|
[1498] | 975 | #ifdef ROAR_FDATASYNC |
---|
[1171] | 976 | ROAR_FDATASYNC(fh); |
---|
[1498] | 977 | #endif |
---|
[1125] | 978 | |
---|
| 979 | return 0; |
---|
[1117] | 980 | } else { |
---|
| 981 | return roar_vio_nonblock(&(g_streams[id]->vio), sync); |
---|
| 982 | } |
---|
[1116] | 983 | } |
---|
| 984 | |
---|
[1928] | 985 | int streams_set_mmap (int id, int reset) { |
---|
| 986 | int use = !reset; |
---|
| 987 | |
---|
[2734] | 988 | _CHECK_SID(id); |
---|
[1928] | 989 | |
---|
| 990 | return roar_vio_ctl(&(g_streams[id]->vio), ROAR_VIO_CTL_SET_UMMAP, &use); |
---|
| 991 | } |
---|
| 992 | |
---|
[4537] | 993 | int streams_set_single_sink(int id, int reset) { |
---|
[4543] | 994 | struct roar_stream_server * self_ss, * ss; |
---|
| 995 | struct roar_stream * s; |
---|
| 996 | int mixer; |
---|
| 997 | size_t count = 0; |
---|
| 998 | int i; |
---|
| 999 | int ssdir; |
---|
| 1000 | |
---|
| 1001 | /* FIXME: This code only uses one refc for the mixer shared with the inputs. |
---|
| 1002 | * If the flag is unset on the mixer but got set by the sources the counter |
---|
| 1003 | * may/will become corrupted. |
---|
| 1004 | */ |
---|
| 1005 | |
---|
[4537] | 1006 | _CHECK_SID(id); |
---|
| 1007 | |
---|
[4543] | 1008 | self_ss = g_streams[id]; |
---|
| 1009 | |
---|
| 1010 | mixer = self_ss->mixer_stream; |
---|
| 1011 | |
---|
[4547] | 1012 | ROAR_DBG("streams_set_single_sink(id=%i, reset=%i): mixer=%i", id, reset, mixer); |
---|
[4545] | 1013 | |
---|
[4546] | 1014 | if ( !reset && id != mixer ) |
---|
| 1015 | if ( self_ss->flags & ROAR_FLAG_SINGLESINK ) |
---|
| 1016 | return 0; |
---|
| 1017 | |
---|
[4543] | 1018 | if ( id == mixer ) { |
---|
| 1019 | if ( reset ) { |
---|
[4545] | 1020 | if ( self_ss->single_sink_self_c ) { |
---|
[4547] | 1021 | ROAR_DBG("streams_set_single_sink(id=%i, reset=%i): mixer=%i", id, reset, mixer); |
---|
[4545] | 1022 | self_ss->single_sink_self_c--; |
---|
[4543] | 1023 | self_ss->single_sink_c--; |
---|
[4545] | 1024 | if ( self_ss->single_sink_c == 0 ) { |
---|
[4547] | 1025 | ROAR_DBG("streams_set_single_sink(id=%i, reset=%i): mixer=%i", id, reset, mixer); |
---|
[4545] | 1026 | self_ss->flags |= ROAR_FLAG_SINGLESINK; |
---|
| 1027 | self_ss->flags -= ROAR_FLAG_SINGLESINK; |
---|
| 1028 | } |
---|
[4543] | 1029 | return 0; |
---|
[4545] | 1030 | } else { |
---|
| 1031 | return -1; |
---|
[4543] | 1032 | } |
---|
| 1033 | } else { |
---|
| 1034 | if ( self_ss->single_sink_c ) { |
---|
[4545] | 1035 | self_ss->single_sink_self_c++; |
---|
[4543] | 1036 | self_ss->single_sink_c++; |
---|
| 1037 | return 0; |
---|
| 1038 | } |
---|
| 1039 | } |
---|
| 1040 | } else if ( reset ) { |
---|
[4547] | 1041 | ROAR_DBG("streams_set_single_sink(id=%i, reset=%i): mixer=%i", id, reset, mixer); |
---|
[4545] | 1042 | |
---|
[4689] | 1043 | if ( (ss = g_streams[mixer]) == NULL ) |
---|
| 1044 | return -1; |
---|
| 1045 | |
---|
| 1046 | if ( ss->single_sink_c == 0 ) |
---|
[4543] | 1047 | return -1; |
---|
| 1048 | ss->single_sink_c--; |
---|
| 1049 | |
---|
| 1050 | if ( ss->single_sink_c == 0 ) { |
---|
| 1051 | self_ss->flags |= ROAR_FLAG_SINGLESINK; |
---|
| 1052 | self_ss->flags -= ROAR_FLAG_SINGLESINK; |
---|
| 1053 | ss->flags |= ROAR_FLAG_SINGLESINK; |
---|
| 1054 | ss->flags -= ROAR_FLAG_SINGLESINK; |
---|
| 1055 | } |
---|
| 1056 | |
---|
| 1057 | return 0; |
---|
| 1058 | } |
---|
| 1059 | |
---|
| 1060 | |
---|
| 1061 | |
---|
| 1062 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 1063 | if ( (s = ROAR_STREAM((ss = g_streams[i]))) != NULL ) { |
---|
| 1064 | if ( s->dir == ROAR_DIR_THRU ) |
---|
[4544] | 1065 | if ( s->pos_rel_id == id || s->pos_rel_id == mixer || g_streams[s->pos_rel_id]->mixer_stream == mixer ) |
---|
[4543] | 1066 | return -1; |
---|
| 1067 | |
---|
| 1068 | if ( ss->mixer_stream != mixer ) |
---|
| 1069 | continue; |
---|
| 1070 | |
---|
| 1071 | ssdir = streams_get_ssdir(i); |
---|
| 1072 | |
---|
[4544] | 1073 | if ( ssdir & STREAM_DIR_OUT ) |
---|
[4543] | 1074 | count++; |
---|
| 1075 | } |
---|
| 1076 | } |
---|
| 1077 | |
---|
| 1078 | if ( count > 1 ) |
---|
| 1079 | return -1; |
---|
| 1080 | |
---|
| 1081 | ss = g_streams[mixer]; |
---|
| 1082 | |
---|
[4545] | 1083 | if ( id == mixer ) |
---|
| 1084 | ss->single_sink_self_c++; |
---|
| 1085 | |
---|
[4543] | 1086 | ss->single_sink_c++; |
---|
| 1087 | |
---|
| 1088 | self_ss->flags |= ROAR_FLAG_SINGLESINK; |
---|
| 1089 | ss->flags |= ROAR_FLAG_SINGLESINK; |
---|
| 1090 | |
---|
| 1091 | return 0; |
---|
[4537] | 1092 | } |
---|
| 1093 | |
---|
[4550] | 1094 | int streams_set_flag (int id, uint32_t flag) { |
---|
[2626] | 1095 | int parent; |
---|
[4815] | 1096 | int tmp; |
---|
[2626] | 1097 | |
---|
[4846] | 1098 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1099 | |
---|
[2734] | 1100 | _CHECK_SID(id); |
---|
[1029] | 1101 | |
---|
[4846] | 1102 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1103 | |
---|
[2952] | 1104 | if ( flag & ROAR_FLAG_IMMUTABLE ) |
---|
| 1105 | flag |= ROAR_FLAG_PRIMARY; |
---|
| 1106 | |
---|
[4552] | 1107 | |
---|
[4846] | 1108 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1109 | |
---|
[4552] | 1110 | // check if flags we are going to change are protected: |
---|
| 1111 | if ( g_streams[id]->flags_protection & flag ) |
---|
| 1112 | return -1; |
---|
| 1113 | |
---|
[4846] | 1114 | flag |= g_streams[id]->flags; |
---|
| 1115 | flag -= g_streams[id]->flags; |
---|
| 1116 | |
---|
| 1117 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
[4552] | 1118 | |
---|
[4540] | 1119 | if ( flag & ROAR_FLAG_MMAP ) { |
---|
| 1120 | if ( streams_set_mmap(id, 0) == -1 ) { |
---|
[1928] | 1121 | flag -= ROAR_FLAG_MMAP; |
---|
[4540] | 1122 | return -1; |
---|
| 1123 | } |
---|
| 1124 | } |
---|
[1926] | 1125 | |
---|
[4846] | 1126 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1127 | |
---|
[1043] | 1128 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 1129 | streams_set_primary(id, 1); |
---|
| 1130 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 1131 | } |
---|
| 1132 | |
---|
[4846] | 1133 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1134 | |
---|
[4537] | 1135 | if ( flag & ROAR_FLAG_SINGLESINK ) { |
---|
[4540] | 1136 | if ( streams_set_single_sink(id, 0) == -1 ) { |
---|
| 1137 | return -1; |
---|
| 1138 | } |
---|
[4545] | 1139 | flag -= ROAR_FLAG_SINGLESINK; |
---|
[4537] | 1140 | } |
---|
| 1141 | |
---|
[4846] | 1142 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1143 | |
---|
[2626] | 1144 | if ( flag & ROAR_FLAG_VIRTUAL ) { |
---|
| 1145 | if ( (parent = ROAR_STREAM(g_streams[id])->pos_rel_id) == -1 ) |
---|
| 1146 | return -1; |
---|
| 1147 | |
---|
| 1148 | if ( streams_ctl(parent, ROAR_CODECFILTER_CTL_VIRTUAL_NEW|ROAR_STREAM_CTL_TYPE_INT, &id) == -1 ) { |
---|
| 1149 | // flag -= ROAR_FLAG_VIRTUAL; |
---|
| 1150 | return -1; |
---|
| 1151 | } |
---|
[2705] | 1152 | |
---|
| 1153 | if ( client_stream_move(streams_get_client(parent), id) == -1 ) { |
---|
| 1154 | return -1; |
---|
| 1155 | } |
---|
[2626] | 1156 | } |
---|
| 1157 | |
---|
[4846] | 1158 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1159 | |
---|
[1116] | 1160 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[4846] | 1161 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
[1908] | 1162 | switch (ROAR_STREAM(g_streams[id])->dir) { |
---|
| 1163 | // for this stream types the flag is used in the subsystem: |
---|
| 1164 | case ROAR_DIR_BRIDGE: |
---|
| 1165 | case ROAR_DIR_MIDI_OUT: |
---|
| 1166 | break; |
---|
| 1167 | |
---|
| 1168 | // normal behavor (vio blocking): |
---|
| 1169 | default: |
---|
[4846] | 1170 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
[2341] | 1171 | // the fh is updated as soon as the fh get ready in case the default ask to set sync |
---|
[4846] | 1172 | if ( g_streams[id]->ready || (!g_streams[id]->ready && !(g_config->streams[ROAR_STREAM(g_streams[id])->dir].flags & ROAR_FLAG_SYNC)) ) { |
---|
| 1173 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1174 | if ( streams_set_sync(id, 1) == -1 ) { |
---|
| 1175 | ROAR_WARN("streams_set_flag(id=%i, flag=0x%.8X): Can not set SYNC flag.", id, flag); |
---|
[2341] | 1176 | flag -= ROAR_FLAG_SYNC; |
---|
[4846] | 1177 | } |
---|
[2341] | 1178 | } |
---|
[4846] | 1179 | break; |
---|
| 1180 | } |
---|
| 1181 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1182 | |
---|
| 1183 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
| 1184 | g_streams[id]->flags |= ROAR_FLAG_SYNC; |
---|
| 1185 | streams_change_sync_num(id, +1); |
---|
[1908] | 1186 | } |
---|
[1116] | 1187 | } |
---|
| 1188 | |
---|
[4846] | 1189 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1190 | |
---|
[1585] | 1191 | if ( flag & ROAR_FLAG_HWMIXER ) { // currently not supported -> ignored |
---|
[1590] | 1192 | g_streams[id]->flags |= flag; |
---|
| 1193 | if ( streams_set_mixer(id) == -1 ) { |
---|
| 1194 | g_streams[id]->flags -= flag; |
---|
| 1195 | return -1; |
---|
| 1196 | } |
---|
[1585] | 1197 | } |
---|
| 1198 | |
---|
[4846] | 1199 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1200 | |
---|
[2417] | 1201 | if ( flag & ROAR_FLAG_RECSOURCE ) { |
---|
| 1202 | if ( streams_recsource_id != -1 ) { |
---|
| 1203 | if ( streams_reset_flag(streams_recsource_id, ROAR_FLAG_RECSOURCE) == -1 ) |
---|
| 1204 | return -1; |
---|
| 1205 | } |
---|
| 1206 | |
---|
[4815] | 1207 | if ( g_streams[id]->ready ) { |
---|
| 1208 | tmp = 1; |
---|
| 1209 | if ( stream_vio_ctl(id, ROAR_VIO_CTL_SET_RECORD, &tmp) == -1 ) |
---|
| 1210 | return -1; |
---|
| 1211 | } |
---|
| 1212 | |
---|
[2417] | 1213 | streams_recsource_id = id; |
---|
| 1214 | } |
---|
| 1215 | |
---|
[4846] | 1216 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1217 | |
---|
[1029] | 1218 | g_streams[id]->flags |= flag; |
---|
| 1219 | |
---|
[1498] | 1220 | #ifdef ROAR_SUPPORT_META |
---|
[1043] | 1221 | if ( flag & ROAR_FLAG_META ) |
---|
| 1222 | stream_meta_finalize(id); |
---|
[1498] | 1223 | #endif |
---|
[1043] | 1224 | |
---|
[4847] | 1225 | ROAR_DBG("streams_set_flag(id=%i, flag=0x%.8X) = 0", id, flag); |
---|
[4846] | 1226 | |
---|
[1029] | 1227 | return 0; |
---|
| 1228 | } |
---|
| 1229 | |
---|
[4550] | 1230 | int streams_set_rawflag (int id, uint32_t flag) { |
---|
[2734] | 1231 | _CHECK_SID(id); |
---|
[2625] | 1232 | |
---|
| 1233 | g_streams[id]->flags |= flag; |
---|
| 1234 | |
---|
[4846] | 1235 | if ( flag & ROAR_FLAG_SYNC ) |
---|
| 1236 | streams_change_sync_num(id, +1); |
---|
| 1237 | |
---|
[2625] | 1238 | return 0; |
---|
| 1239 | } |
---|
| 1240 | |
---|
[4550] | 1241 | int streams_reset_flag (int id, uint32_t flag) { |
---|
[4815] | 1242 | int tmp; |
---|
| 1243 | |
---|
[4847] | 1244 | ROAR_DBG("streams_reset_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1245 | |
---|
[2734] | 1246 | _CHECK_SID(id); |
---|
[1029] | 1247 | |
---|
[4552] | 1248 | // check if flags we are going to change are protected: |
---|
| 1249 | if ( g_streams[id]->flags_protection & flag ) |
---|
| 1250 | return -1; |
---|
| 1251 | |
---|
[2952] | 1252 | if ( g_streams[id]->flags & ROAR_FLAG_IMMUTABLE ) { |
---|
| 1253 | flag |= ROAR_FLAG_PRIMARY; |
---|
| 1254 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 1255 | } |
---|
| 1256 | |
---|
[4846] | 1257 | flag &= g_streams[id]->flags; |
---|
| 1258 | |
---|
[4847] | 1259 | ROAR_DBG("streams_reset_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1260 | |
---|
[4815] | 1261 | if ( (flag & ROAR_FLAG_RECSOURCE) && streams_recsource_id == id ) { |
---|
| 1262 | if ( g_streams[id]->ready ) { |
---|
| 1263 | tmp = 0; |
---|
[4847] | 1264 | if ( stream_vio_ctl(id, ROAR_VIO_CTL_SET_RECORD, &tmp) == -1 ) { |
---|
| 1265 | if ( alive ) { // do not warn if we are shutting down anyway. |
---|
| 1266 | ROAR_WARN("streams_reset_flag(id=%i, flag=0x%.8X): Driver refused to reset record flag.", id, flag); |
---|
| 1267 | } |
---|
| 1268 | ROAR_DBG("streams_reset_flag(id=%i, flag=0x%.8X) = -1", id, flag); |
---|
[4815] | 1269 | return -1; |
---|
[4847] | 1270 | } |
---|
[4815] | 1271 | } |
---|
| 1272 | |
---|
| 1273 | streams_recsource_id = -1; |
---|
| 1274 | } |
---|
| 1275 | |
---|
[4847] | 1276 | ROAR_DBG("streams_reset_flag(id=%i, flag=0x%.8X) = ?", id, flag); |
---|
| 1277 | |
---|
[2417] | 1278 | |
---|
[4540] | 1279 | if ( flag & ROAR_FLAG_MMAP ) { |
---|
| 1280 | if ( streams_set_mmap(id, 1) == -1 ) { |
---|
[1928] | 1281 | flag -= ROAR_FLAG_MMAP; |
---|
[4540] | 1282 | return -1; |
---|
| 1283 | } |
---|
| 1284 | } |
---|
[1928] | 1285 | |
---|
[1043] | 1286 | if ( flag & ROAR_FLAG_PRIMARY ) { |
---|
| 1287 | streams_set_primary(id, 0); |
---|
| 1288 | flag -= ROAR_FLAG_PRIMARY; |
---|
| 1289 | } |
---|
| 1290 | |
---|
[4537] | 1291 | if ( flag & ROAR_FLAG_SINGLESINK ) { |
---|
[4540] | 1292 | if ( streams_set_single_sink(id, 1) == -1 ) { |
---|
| 1293 | return -1; |
---|
| 1294 | } |
---|
[4545] | 1295 | flag -= ROAR_FLAG_SINGLESINK; |
---|
[4537] | 1296 | } |
---|
| 1297 | |
---|
[1116] | 1298 | if ( flag & ROAR_FLAG_SYNC ) { |
---|
[2264] | 1299 | // we refuse to reset the flag on FILTER streams |
---|
| 1300 | if ( streams_get_dir(id) == ROAR_DIR_FILTER ) { |
---|
| 1301 | // flags -= ROAR_FLAG_SYNC; |
---|
| 1302 | return -1; |
---|
| 1303 | } else { |
---|
[4846] | 1304 | if ( streams_set_sync(id, 0) == -1 ) |
---|
| 1305 | return -1; |
---|
| 1306 | |
---|
| 1307 | g_streams[id]->flags |= ROAR_FLAG_SYNC; |
---|
| 1308 | g_streams[id]->flags -= ROAR_FLAG_SYNC; |
---|
| 1309 | flag -= ROAR_FLAG_SYNC; |
---|
| 1310 | streams_change_sync_num(id, -1); |
---|
[2264] | 1311 | } |
---|
[1116] | 1312 | } |
---|
| 1313 | |
---|
[1029] | 1314 | g_streams[id]->flags |= flag; |
---|
| 1315 | g_streams[id]->flags -= flag; |
---|
| 1316 | |
---|
[4847] | 1317 | ROAR_DBG("streams_reset_flag(id=%i, flag=0x%.8X) = 0", id, flag); |
---|
| 1318 | |
---|
[1029] | 1319 | return 0; |
---|
| 1320 | } |
---|
| 1321 | |
---|
[4550] | 1322 | int streams_get_flag (int id, uint32_t flag) { |
---|
[2734] | 1323 | _CHECK_SID(id); |
---|
[1042] | 1324 | |
---|
| 1325 | return g_streams[id]->flags & flag ? 1 : 0; |
---|
| 1326 | } |
---|
| 1327 | |
---|
[4552] | 1328 | int streams_protect_flag (int id, uint32_t flag) { |
---|
| 1329 | _CHECK_SID(id); |
---|
| 1330 | |
---|
| 1331 | g_streams[id]->flags_protection |= flag; |
---|
| 1332 | |
---|
| 1333 | return 0; |
---|
| 1334 | } |
---|
| 1335 | |
---|
[1842] | 1336 | int streams_set_name (int id, char * name) { |
---|
| 1337 | char * str; |
---|
| 1338 | |
---|
[2734] | 1339 | _CHECK_SID(id); |
---|
[1842] | 1340 | |
---|
[4957] | 1341 | if ( (str = roar_mm_strdup(name)) == NULL ) |
---|
[1842] | 1342 | return -1; |
---|
| 1343 | |
---|
| 1344 | if ( g_streams[id]->name != NULL ) |
---|
[4957] | 1345 | roar_mm_free(g_streams[id]->name); |
---|
[1842] | 1346 | |
---|
| 1347 | g_streams[id]->name = str; |
---|
[1845] | 1348 | |
---|
| 1349 | return 0; |
---|
[1842] | 1350 | } |
---|
| 1351 | |
---|
| 1352 | char * streams_get_name (int id) { |
---|
[2734] | 1353 | _CHECK_SID_RET(id, NULL); |
---|
[1842] | 1354 | |
---|
| 1355 | return g_streams[id]->name; |
---|
| 1356 | } |
---|
| 1357 | |
---|
| 1358 | |
---|
[1223] | 1359 | int streams_calc_delay (int id) { |
---|
[1142] | 1360 | struct roar_stream_server * ss; |
---|
[1151] | 1361 | struct roar_stream * s; |
---|
[1142] | 1362 | register uint_least32_t d = 0; |
---|
| 1363 | uint_least32_t t[1]; |
---|
[1151] | 1364 | uint64_t tmp; |
---|
[1142] | 1365 | |
---|
[2734] | 1366 | _CHECK_SID(id); |
---|
| 1367 | |
---|
[1151] | 1368 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
[1142] | 1369 | return -1; |
---|
| 1370 | |
---|
[3212] | 1371 | // mixer store there value in ss->delay directly |
---|
| 1372 | if ( s->dir == ROAR_DIR_MIXING ) |
---|
| 1373 | return 0; |
---|
| 1374 | |
---|
[4157] | 1375 | #if 0 |
---|
| 1376 | // this only confuses the user... |
---|
[3216] | 1377 | if ( ss->mixer_stream != id ) { |
---|
| 1378 | if ( streams_calc_delay(ss->mixer_stream) != -1 ) { |
---|
| 1379 | d += g_streams[ss->mixer_stream]->delay; // in case we can calc the delay |
---|
| 1380 | // the stream must exist, so no check here |
---|
| 1381 | } |
---|
| 1382 | } |
---|
[4157] | 1383 | #endif |
---|
[3216] | 1384 | |
---|
[1142] | 1385 | if ( ss->codecfilter != -1 ) { |
---|
| 1386 | if ( codecfilter_delay(ss->codecfilter_inst, ss->codecfilter, t) != -1 ) |
---|
| 1387 | d += *t; |
---|
| 1388 | } |
---|
| 1389 | |
---|
[1151] | 1390 | if ( ss->vio.ctl != NULL ) { |
---|
| 1391 | if ( roar_vio_ctl(&(ss->vio), ROAR_VIO_CTL_GET_DELAY, t) != -1 ) { // *t is in byte |
---|
[1223] | 1392 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in byte: %i", id, *t); |
---|
[1151] | 1393 | tmp = *t; |
---|
| 1394 | tmp *= 1000000; // musec per sec |
---|
| 1395 | tmp /= s->info.rate * s->info.channels * (s->info.bits/8); |
---|
[2387] | 1396 | ROAR_DBG("streams_calc_delay(id=%i): VIO delay in musec: %llu", id, tmp); |
---|
[1151] | 1397 | |
---|
| 1398 | d += tmp; |
---|
| 1399 | } |
---|
| 1400 | } |
---|
| 1401 | |
---|
[1223] | 1402 | ROAR_DBG("streams_calc_delay(id=%i): delay in musec: %i", id, d); |
---|
[1151] | 1403 | |
---|
[1142] | 1404 | ss->delay = d; |
---|
| 1405 | |
---|
| 1406 | return 0; |
---|
| 1407 | } |
---|
| 1408 | |
---|
[1590] | 1409 | int streams_set_mixer (int id) { |
---|
| 1410 | struct roar_stream_server * ss; |
---|
[2416] | 1411 | struct roar_stream_server * pmss; |
---|
| 1412 | int i; |
---|
| 1413 | int subsys; |
---|
[4383] | 1414 | int tsubsys; |
---|
[1590] | 1415 | |
---|
[2734] | 1416 | _CHECK_SID(id); |
---|
| 1417 | |
---|
[1590] | 1418 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1419 | return -1; |
---|
| 1420 | |
---|
[2416] | 1421 | if ( streams_get_flag(id, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
| 1422 | if ( (subsys = streams_get_subsys(id)) == -1 ) |
---|
| 1423 | return -1; |
---|
| 1424 | |
---|
| 1425 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 1426 | if ( (pmss = g_streams[i]) != NULL ) { |
---|
| 1427 | if ( streams_get_flag(i, ROAR_FLAG_PASSMIXER) == 1 ) { |
---|
[4383] | 1428 | tsubsys = streams_get_subsys(i); |
---|
| 1429 | if ( tsubsys == subsys || tsubsys == ROAR_SUBSYS_NONE ) { |
---|
[4119] | 1430 | if ( &(pmss->mixer) != &(ss->mixer) ) { |
---|
[5257] | 1431 | if ( roar_conv_volume(&(pmss->mixer), &(ss->mixer), |
---|
| 1432 | ROAR_STREAM(pmss)->info.channels, ROAR_STREAM(ss)->info.channels) == -1 ) { |
---|
| 1433 | ROAR_WARN("streams_set_mixer(id=%i): Can not convert mixer settings for stream %i", id, i); |
---|
| 1434 | } |
---|
[4119] | 1435 | } |
---|
[2416] | 1436 | |
---|
| 1437 | // update hwmixers and the like but do not set mixer value recrusivly. |
---|
| 1438 | streams_reset_flag(i, ROAR_FLAG_PASSMIXER); |
---|
| 1439 | streams_set_mixer(i); |
---|
| 1440 | streams_set_flag(i, ROAR_FLAG_PASSMIXER); |
---|
| 1441 | } |
---|
| 1442 | } |
---|
| 1443 | } |
---|
| 1444 | } |
---|
| 1445 | } |
---|
| 1446 | |
---|
[4417] | 1447 | roar_notify_core_emit_snoargs(ROAR_OE_STREAM_CHANGE_VOLUME, -1, id, ROAR_OT_STREAM); |
---|
| 1448 | |
---|
[1590] | 1449 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) |
---|
| 1450 | return 0; |
---|
| 1451 | |
---|
[4365] | 1452 | if ( ss->driver_id != -1 ) { |
---|
| 1453 | return driver_set_volume(id, &(ss->mixer)); |
---|
[4753] | 1454 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
[4365] | 1455 | } else if ( ss->mixerstream != NULL ) { |
---|
| 1456 | return hwmixer_set_volume(id, ss, ss->mixerstream, &(ss->mixer)); |
---|
[4753] | 1457 | #endif |
---|
[4365] | 1458 | } else { |
---|
[1590] | 1459 | return 0; |
---|
[4365] | 1460 | } |
---|
[1590] | 1461 | } |
---|
| 1462 | |
---|
[3542] | 1463 | int streams_set_map (int id, char * map, size_t len) { |
---|
| 1464 | struct roar_stream_server * ss; |
---|
| 1465 | int ssdir; |
---|
| 1466 | |
---|
| 1467 | _CHECK_SID(id); |
---|
| 1468 | |
---|
| 1469 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1470 | return -1; |
---|
| 1471 | |
---|
| 1472 | if ( map != NULL ) { |
---|
| 1473 | if ( ROAR_STREAM(ss)->info.channels != len ) |
---|
| 1474 | return -1; |
---|
| 1475 | } |
---|
| 1476 | |
---|
[3557] | 1477 | ssdir = streams_get_ssdir(id); |
---|
| 1478 | |
---|
[3542] | 1479 | switch (ssdir) { |
---|
| 1480 | case STREAM_DIR_IN: |
---|
[3557] | 1481 | case STREAM_DIR_NONE: |
---|
[3671] | 1482 | memset(ss->chanmap.in, 0, sizeof(ss->chanmap.in)); |
---|
| 1483 | |
---|
[3556] | 1484 | if ( map != NULL ) |
---|
| 1485 | memcpy(ss->chanmap.in, map, len); |
---|
| 1486 | |
---|
[3542] | 1487 | roardsp_chanmap_calc(&(ss->chanmap), ROARDSP_CHANMAP_MAP, 0); |
---|
| 1488 | break; |
---|
| 1489 | case STREAM_DIR_OUT: |
---|
[3671] | 1490 | memset(ss->chanmap.out, 0, sizeof(ss->chanmap.out)); |
---|
| 1491 | |
---|
[3556] | 1492 | if ( map != NULL ) |
---|
| 1493 | memcpy(ss->chanmap.out, map, len); |
---|
| 1494 | |
---|
| 1495 | roardsp_chanmap_calc(&(ss->chanmap), ROARDSP_CHANMAP_MAP, 0); |
---|
[3542] | 1496 | break; |
---|
[3557] | 1497 | default: |
---|
| 1498 | return -1; |
---|
[3542] | 1499 | } |
---|
| 1500 | |
---|
| 1501 | return 0; |
---|
| 1502 | } |
---|
| 1503 | |
---|
[4272] | 1504 | int streams_ltm_ctl (int id, int mt, int window, int cmd) { |
---|
| 1505 | struct roar_stream_server * ss; |
---|
| 1506 | struct roar_stream_ltm * ltm; |
---|
| 1507 | int i; |
---|
| 1508 | |
---|
| 1509 | _CHECK_SID(id); |
---|
| 1510 | |
---|
| 1511 | // currently we only support the WORKBLOCK window |
---|
| 1512 | if ( window != ROAR_LTM_WIN_WORKBLOCK ) |
---|
| 1513 | return -1; |
---|
| 1514 | |
---|
| 1515 | // currently we only support ROAR_LTM_MT_RMS |
---|
| 1516 | if ( (mt | ROAR_LTM_MT_RMS) - ROAR_LTM_MT_RMS ) |
---|
| 1517 | return -1; |
---|
| 1518 | |
---|
| 1519 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1520 | return -1; |
---|
| 1521 | |
---|
| 1522 | switch (cmd) { |
---|
| 1523 | case ROAR_LTM_SST_NOOP: |
---|
| 1524 | return 0; |
---|
| 1525 | break; |
---|
| 1526 | case ROAR_LTM_SST_REGISTER: |
---|
| 1527 | // test if the window is already registered: |
---|
| 1528 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
| 1529 | ltm = &(ss->ltm[i]); |
---|
| 1530 | if ( ltm->refc && ltm->window == window ) { |
---|
| 1531 | ltm->refc++; |
---|
| 1532 | ltm->mt |= mt; |
---|
| 1533 | return 0; |
---|
| 1534 | } |
---|
| 1535 | } |
---|
| 1536 | |
---|
| 1537 | // register |
---|
| 1538 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
| 1539 | ltm = &(ss->ltm[i]); |
---|
| 1540 | if ( ltm->refc == 0 ) { |
---|
| 1541 | memset(ltm, 0, sizeof(struct roar_stream_ltm)); |
---|
| 1542 | ltm->refc = 1; |
---|
| 1543 | ltm->window = window; |
---|
| 1544 | ltm->parent_window = -1; |
---|
| 1545 | ltm->mt = mt; |
---|
| 1546 | ltm->channels = ROAR_STREAM(ss)->info.channels; |
---|
| 1547 | if ( (ltm->cur = roar_mm_malloc(ltm->channels*sizeof(struct roar_ltm_vals))) == NULL ) { |
---|
| 1548 | ltm->refc = 0; // reset |
---|
| 1549 | return -1; |
---|
| 1550 | } |
---|
| 1551 | memset(ltm->cur, 0, ltm->channels*sizeof(struct roar_ltm_vals)); |
---|
| 1552 | return 0; |
---|
| 1553 | } |
---|
| 1554 | } |
---|
| 1555 | return -1; |
---|
| 1556 | break; |
---|
| 1557 | case ROAR_LTM_SST_UNREGISTER: |
---|
| 1558 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
| 1559 | ltm = &(ss->ltm[i]); |
---|
| 1560 | if ( ltm->refc && ltm->window == window ) { |
---|
| 1561 | ltm->refc--; |
---|
| 1562 | if ( ! ltm->refc ) { |
---|
| 1563 | roar_mm_free(ltm->cur); |
---|
| 1564 | return 0; |
---|
| 1565 | } |
---|
| 1566 | } |
---|
| 1567 | } |
---|
| 1568 | return -1; |
---|
| 1569 | break; |
---|
| 1570 | default: |
---|
| 1571 | return -1; |
---|
| 1572 | break; |
---|
| 1573 | } |
---|
| 1574 | } |
---|
| 1575 | |
---|
| 1576 | int streams_ltm_calc (int id, struct roar_audio_info * info, void * data, size_t len) { |
---|
| 1577 | struct roar_stream_server * ss; |
---|
| 1578 | struct roar_stream_ltm * ltm = NULL; |
---|
| 1579 | int64_t rmsbuf_real[8]; |
---|
| 1580 | int64_t * rmsbuf = rmsbuf_real; |
---|
| 1581 | size_t samples = 0; |
---|
| 1582 | int i; |
---|
| 1583 | |
---|
| 1584 | _CHECK_SID(id); |
---|
| 1585 | |
---|
| 1586 | if ( data == NULL || info == NULL ) |
---|
| 1587 | return -1; |
---|
| 1588 | |
---|
| 1589 | if ( info->codec != ROAR_CODEC_DEFAULT ) |
---|
| 1590 | return -1; |
---|
| 1591 | |
---|
| 1592 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1593 | return -1; |
---|
| 1594 | |
---|
| 1595 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
| 1596 | if ( ss->ltm[i].refc && ss->ltm[i].window == ROAR_LTM_WIN_WORKBLOCK ) { |
---|
| 1597 | ltm = &(ss->ltm[i]); |
---|
| 1598 | break; |
---|
| 1599 | } |
---|
| 1600 | } |
---|
| 1601 | |
---|
| 1602 | if ( ltm == NULL ) |
---|
| 1603 | return -1; |
---|
| 1604 | |
---|
| 1605 | // TODO: support change of number of channels |
---|
| 1606 | if ( ltm->channels != info->channels ) |
---|
| 1607 | return -1; |
---|
| 1608 | |
---|
| 1609 | if ( len == 0 ) { |
---|
| 1610 | memset(ltm->cur, 0, ltm->channels * sizeof(struct roar_ltm_vals)); |
---|
| 1611 | return 0; |
---|
| 1612 | } |
---|
| 1613 | |
---|
| 1614 | // TODO: support more channels then rmsbuf_real has space for |
---|
| 1615 | if ( ltm->channels > (sizeof(rmsbuf_real)/sizeof(*rmsbuf_real)) ) { |
---|
| 1616 | return -1; |
---|
| 1617 | } |
---|
| 1618 | |
---|
| 1619 | samples = len / info->bits; |
---|
| 1620 | |
---|
| 1621 | if ( ltm->mt & ROAR_LTM_MT_RMS ) { |
---|
| 1622 | if ( roar_rms2_1_b_n(data, samples, rmsbuf, ltm->channels, info->bits) == -1 ) |
---|
| 1623 | return -1; |
---|
| 1624 | |
---|
| 1625 | for (i = 0; i < ltm->channels; i++) { |
---|
[4286] | 1626 | ltm->cur[i].rms = rmsbuf[i] << (64 - info->bits*2); |
---|
[4272] | 1627 | } |
---|
| 1628 | |
---|
| 1629 | ROAR_DBG("streams_ltm_calc(id=%i,...): rmsbuf[0]=%lli", id, (long long int)rmsbuf[0]); |
---|
| 1630 | } |
---|
| 1631 | |
---|
| 1632 | return 0; |
---|
| 1633 | } |
---|
| 1634 | |
---|
[4289] | 1635 | struct roar_stream_ltm * streams_ltm_get(int id, int mt, int window) { |
---|
[4278] | 1636 | struct roar_stream_server * ss; |
---|
| 1637 | struct roar_stream_ltm * ltm = NULL; |
---|
| 1638 | int i; |
---|
| 1639 | |
---|
[4290] | 1640 | _CHECK_SID_RET(id, NULL); |
---|
[4278] | 1641 | |
---|
| 1642 | if ( (ss = g_streams[id]) == NULL ) |
---|
[4290] | 1643 | return NULL; |
---|
[4278] | 1644 | |
---|
| 1645 | for (i = 0; i < MAX_LTM_WINDOWS_PER_STREAM; i++) { |
---|
| 1646 | if ( ss->ltm[i].refc && ss->ltm[i].window == window ) { |
---|
| 1647 | ltm = &(ss->ltm[i]); |
---|
| 1648 | break; |
---|
| 1649 | } |
---|
| 1650 | } |
---|
| 1651 | |
---|
| 1652 | if ( ltm == NULL ) |
---|
| 1653 | return NULL; |
---|
| 1654 | |
---|
| 1655 | if ( (ltm->mt & mt) != mt ) |
---|
| 1656 | return NULL; |
---|
| 1657 | |
---|
| 1658 | return ltm; |
---|
| 1659 | } |
---|
[4272] | 1660 | |
---|
[4753] | 1661 | #ifndef ROAR_WITHOUT_DCOMP_MIXER |
---|
[4361] | 1662 | struct hwmixer_stream * streams_get_mixerstream(int id) { |
---|
| 1663 | _CHECK_SID_RET(id, NULL); |
---|
| 1664 | |
---|
| 1665 | return g_streams[id]->mixerstream; |
---|
| 1666 | } |
---|
| 1667 | |
---|
| 1668 | int streams_set_mixerstream(int id, struct hwmixer_stream * mstream) { |
---|
| 1669 | _CHECK_SID(id); |
---|
| 1670 | |
---|
| 1671 | g_streams[id]->mixerstream = mstream; |
---|
| 1672 | |
---|
| 1673 | return 0; |
---|
| 1674 | } |
---|
| 1675 | |
---|
[4379] | 1676 | int streams_read_mixervalues(int id) { |
---|
| 1677 | struct roar_stream_server * ss; |
---|
| 1678 | |
---|
| 1679 | _CHECK_SID(id); |
---|
| 1680 | |
---|
| 1681 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1682 | return -1; |
---|
| 1683 | |
---|
| 1684 | if ( ss->mixerstream == NULL ) |
---|
| 1685 | return -1; |
---|
| 1686 | |
---|
| 1687 | return hwmixer_get_volume(id, ss, ss->mixerstream, &(ss->mixer)); |
---|
| 1688 | } |
---|
[4753] | 1689 | #endif |
---|
[4272] | 1690 | |
---|
[1224] | 1691 | int streams_ctl (int id, int_least32_t cmd, void * data) { |
---|
| 1692 | struct roar_stream_server * ss; |
---|
| 1693 | int_least32_t comp; |
---|
| 1694 | |
---|
[2734] | 1695 | _CHECK_SID(id); |
---|
| 1696 | |
---|
[1224] | 1697 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1698 | return -1; |
---|
| 1699 | |
---|
| 1700 | comp = cmd & ROAR_STREAM_CTL_COMPMASK; |
---|
| 1701 | |
---|
| 1702 | cmd &= ~comp; |
---|
| 1703 | |
---|
[1239] | 1704 | ROAR_DBG("streams_ctl(id=%i, cmd=?, data=%p): comp=0x%.8x, cmd=0x%.4x", id, data, comp, cmd); |
---|
[1238] | 1705 | |
---|
[1224] | 1706 | switch (comp) { |
---|
| 1707 | case ROAR_STREAM_CTL_COMP_BASE: |
---|
| 1708 | break; |
---|
| 1709 | case ROAR_STREAM_CTL_COMP_CF: |
---|
| 1710 | return codecfilter_ctl(ss->codecfilter_inst, ss->codecfilter, cmd, data); |
---|
| 1711 | break; |
---|
| 1712 | case ROAR_STREAM_CTL_COMP_DRV: |
---|
| 1713 | break; |
---|
| 1714 | default: |
---|
| 1715 | return -1; |
---|
| 1716 | } |
---|
| 1717 | |
---|
| 1718 | return -1; |
---|
| 1719 | } |
---|
| 1720 | |
---|
[0] | 1721 | int streams_get_outputbuffer (int id, void ** buffer, size_t size) { |
---|
[2734] | 1722 | _CHECK_SID(id); |
---|
[0] | 1723 | |
---|
| 1724 | // output buffer size does never change. |
---|
| 1725 | if ( g_streams[id]->output != NULL ) { |
---|
| 1726 | *buffer = g_streams[id]->output; |
---|
| 1727 | return 0; |
---|
| 1728 | } |
---|
| 1729 | |
---|
[4957] | 1730 | if ( (g_streams[id]->output = roar_mm_malloc(size)) == NULL ) { |
---|
[0] | 1731 | ROAR_ERR("streams_get_outputbuffer(*): Can not alloc: %s", strerror(errno)); |
---|
| 1732 | return -1; |
---|
| 1733 | } |
---|
| 1734 | |
---|
| 1735 | *buffer = g_streams[id]->output; |
---|
| 1736 | |
---|
| 1737 | return 0; |
---|
| 1738 | } |
---|
| 1739 | |
---|
[2061] | 1740 | int streams_fill_mixbuffer2 (int id, struct roar_audio_info * info) { |
---|
| 1741 | size_t outlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(info); |
---|
| 1742 | void * outdata; |
---|
| 1743 | size_t inlen; |
---|
| 1744 | size_t inlen_got; |
---|
| 1745 | void * indata = NULL; |
---|
[2091] | 1746 | size_t buflen; |
---|
| 1747 | void * bufdata = NULL; |
---|
| 1748 | struct roar_buffer * bufbuf = NULL; |
---|
[2061] | 1749 | int is_the_same = 0; |
---|
| 1750 | struct roar_audio_info * stream_info; |
---|
| 1751 | struct roar_stream * s; |
---|
| 1752 | struct roar_stream_server * ss; |
---|
| 1753 | |
---|
[2734] | 1754 | _CHECK_SID(id); |
---|
| 1755 | |
---|
[2061] | 1756 | if ( (s = ROAR_STREAM(ss = g_streams[id])) == NULL ) |
---|
| 1757 | return -1; |
---|
| 1758 | |
---|
| 1759 | // set up stream_info |
---|
| 1760 | stream_info = &(s->info); |
---|
| 1761 | |
---|
| 1762 | // calc todo_in |
---|
| 1763 | inlen = ROAR_OUTPUT_CALC_OUTBUFSIZE(stream_info); |
---|
| 1764 | |
---|
[2091] | 1765 | buflen = ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(info, stream_info); |
---|
| 1766 | |
---|
[2747] | 1767 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen=%lu, buflen=%lu", id, info, (unsigned long)inlen, (unsigned long)buflen); |
---|
| 1768 | |
---|
[2061] | 1769 | if ( inlen == 0 ) { |
---|
| 1770 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=%p{...}): inlen == 0, this should not happen!", id, info); |
---|
| 1771 | return -1; |
---|
| 1772 | } |
---|
| 1773 | |
---|
| 1774 | if ( streams_get_outputbuffer(id, &outdata, outlen) == -1 ) { |
---|
| 1775 | return -1; |
---|
| 1776 | } |
---|
| 1777 | |
---|
| 1778 | if ( outdata == NULL ) { |
---|
| 1779 | return -1; |
---|
| 1780 | } |
---|
| 1781 | |
---|
| 1782 | ROAR_DBG("streams_fill_mixbuffer2(*): outdata=%p, len=%i->%i (in->out)", outdata, inlen, outlen); |
---|
| 1783 | |
---|
| 1784 | is_the_same = stream_info->rate == info->rate && stream_info->bits == info->bits && |
---|
| 1785 | stream_info->channels == info->channels && stream_info->codec == info->codec; |
---|
| 1786 | |
---|
| 1787 | ROAR_DBG("streams_fill_mixbuffer2(*): is_the_same=%i", is_the_same); |
---|
| 1788 | |
---|
[2091] | 1789 | if ( !is_the_same && buflen > outlen ) { |
---|
| 1790 | /* |
---|
[2061] | 1791 | // this is not supported at the moment |
---|
| 1792 | memset(outdata, 0, outlen); |
---|
| 1793 | return -1; |
---|
[2091] | 1794 | */ |
---|
| 1795 | |
---|
| 1796 | if ( roar_buffer_new(&bufbuf, buflen) == -1 ) |
---|
| 1797 | return -1; |
---|
| 1798 | |
---|
| 1799 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
| 1800 | return -1; |
---|
[2102] | 1801 | indata = bufdata; |
---|
[2061] | 1802 | } else { |
---|
[2091] | 1803 | indata = outdata; |
---|
| 1804 | bufdata = outdata; |
---|
[2061] | 1805 | } |
---|
| 1806 | |
---|
| 1807 | inlen_got = inlen; |
---|
| 1808 | |
---|
[2102] | 1809 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 1810 | |
---|
[2061] | 1811 | if ( stream_shift_out_buffer(id, indata, &inlen_got) == -1 ) { |
---|
| 1812 | if ( ss->is_new ) { |
---|
| 1813 | ss->pre_underruns++; |
---|
[4417] | 1814 | roar_notify_core_emit_simple(ROAR_OE_STREAM_XRUN, -1, id, ROAR_OT_STREAM, ROAR_XRUN_UNDER_PRE, ss->pre_underruns, NULL, 0); |
---|
[2061] | 1815 | } else { |
---|
[2088] | 1816 | ROAR_WARN("streams_fill_mixbuffer2(id=%i, info=...): underrun in stream", id); |
---|
[2061] | 1817 | ss->post_underruns++; |
---|
[4417] | 1818 | roar_notify_core_emit_simple(ROAR_OE_STREAM_XRUN, -1, id, ROAR_OT_STREAM, ROAR_XRUN_UNDER_POST, ss->post_underruns, NULL, 0); |
---|
[2061] | 1819 | } |
---|
| 1820 | memset(outdata, 0, outlen); |
---|
| 1821 | return 0; |
---|
| 1822 | } |
---|
| 1823 | |
---|
[2387] | 1824 | ROAR_DBG("streams_fill_mixbuffer2(id=%i, info=...): inlen_got=%u", id, inlen_got); |
---|
| 1825 | |
---|
[2061] | 1826 | if ( ss->is_new ) { |
---|
[4336] | 1827 | //ROAR_INFO("streams_fill_mixbuffer2(id=%i, info=...): stream state: new->old", ROAR_DBG_INFO_VERBOSE, id); |
---|
| 1828 | _streams_change_state(ss, ROAR_STREAMSTATE_OLD, "streams_fill_mixbuffer2"); |
---|
[2061] | 1829 | } |
---|
| 1830 | |
---|
| 1831 | ss->is_new = 0; |
---|
| 1832 | |
---|
[3549] | 1833 | // check channel map: |
---|
[3555] | 1834 | #if 0 |
---|
[3554] | 1835 | if ( roardsp_chanmap_mappcm(indata, indata, inlen, stream_info->channels, &(ss->chanmap), stream_info->bits) == -1 ) { |
---|
[3549] | 1836 | if ( bufbuf != NULL ) |
---|
| 1837 | roar_buffer_free(bufbuf); |
---|
| 1838 | return -1; |
---|
| 1839 | } |
---|
[3555] | 1840 | #endif |
---|
[3549] | 1841 | |
---|
[4118] | 1842 | // TODO: this only works in case a we have a amp for the given stream parameters. |
---|
| 1843 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) && !streams_get_flag(id, ROAR_FLAG_PASSMIXER) ) { |
---|
| 1844 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_amp_pcm(*)..."); |
---|
[4120] | 1845 | if ( roar_amp_pcm(indata, stream_info->bits, indata, 8*inlen / stream_info->bits, stream_info->channels, &(ss->mixer)) == -1 ) |
---|
[4118] | 1846 | return -1; |
---|
| 1847 | } |
---|
| 1848 | |
---|
[3549] | 1849 | // check codec, bits, channels, rate... |
---|
[2061] | 1850 | if ( is_the_same ) { |
---|
| 1851 | if ( indata != outdata ) |
---|
| 1852 | memcpy(outdata, indata, inlen); |
---|
| 1853 | |
---|
| 1854 | if ( inlen < outlen ) |
---|
| 1855 | memset(outdata+inlen, 0, outlen-inlen); |
---|
| 1856 | } else { |
---|
[2095] | 1857 | // if ( roar_conv(outdata, indata, (8*inlen_got*info->rate)/(stream_info->rate * stream_info->bits), stream_info, info) == -1 ) { |
---|
[2387] | 1858 | ROAR_DBG("streams_fill_mixbuffer2(*): CALL roar_conv2(*)..."); |
---|
[4111] | 1859 | if ( roar_conv2(bufdata, indata, inlen, stream_info, info, outlen) == -1 ) { |
---|
[2091] | 1860 | if ( bufbuf != NULL ) |
---|
| 1861 | roar_buffer_free(bufbuf); |
---|
[2062] | 1862 | return -1; |
---|
| 1863 | } |
---|
| 1864 | |
---|
| 1865 | // memset(outdata, 0, outlen); |
---|
[2061] | 1866 | } |
---|
| 1867 | |
---|
[2091] | 1868 | if ( bufbuf != NULL ) { |
---|
| 1869 | memcpy(outdata, bufdata, outlen); |
---|
| 1870 | roar_buffer_free(bufbuf); |
---|
| 1871 | } |
---|
| 1872 | |
---|
[4272] | 1873 | streams_ltm_calc(id, stream_info, outdata, outlen); |
---|
| 1874 | |
---|
[2153] | 1875 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) { |
---|
[2747] | 1876 | ROAR_DBG("streams_fill_mixbuffer2(*): Calcing antiecho..."); |
---|
[2153] | 1877 | // we can ignore errors here: |
---|
[2389] | 1878 | if ( stream_outputbuffer_request(id, &bufbuf, buflen) == 0 ) { |
---|
[2153] | 1879 | if ( roar_buffer_get_data(bufbuf, &bufdata) != -1 ) |
---|
| 1880 | memcpy(bufdata, outdata, outlen); |
---|
| 1881 | } |
---|
| 1882 | } |
---|
| 1883 | |
---|
[3932] | 1884 | ROAR_DBG("streams_fill_mixbuffer2(*): inlen_got=%llu, stream_info={.bits=%i, .rate=%i,...}", (long long unsigned int)inlen_got, stream_info->bits, stream_info->rate); |
---|
| 1885 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, (inlen_got*8)/stream_info->bits); |
---|
[2153] | 1886 | |
---|
[2747] | 1887 | ROAR_DBG("streams_fill_mixbuffer2(*) = 0"); |
---|
[2062] | 1888 | return 0; |
---|
[2061] | 1889 | } |
---|
| 1890 | |
---|
[491] | 1891 | |
---|
[0] | 1892 | int streams_get_mixbuffers (void *** bufferlist, struct roar_audio_info * info, unsigned int pos) { |
---|
[84] | 1893 | static void * bufs[ROAR_STREAMS_MAX+1]; |
---|
[0] | 1894 | int i; |
---|
| 1895 | int have = 0; |
---|
[2458] | 1896 | int dir; |
---|
[0] | 1897 | |
---|
| 1898 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 1899 | if ( g_streams[i] != NULL ) { |
---|
[2458] | 1900 | dir = streams_get_dir(i); |
---|
| 1901 | |
---|
| 1902 | switch (dir) { |
---|
| 1903 | case ROAR_DIR_PLAY: |
---|
| 1904 | case ROAR_DIR_BIDIR: |
---|
| 1905 | break; |
---|
[2459] | 1906 | case ROAR_DIR_BRIDGE: |
---|
| 1907 | if ( g_streams[i]->buffer == NULL ) |
---|
| 1908 | continue; |
---|
| 1909 | break; |
---|
[2458] | 1910 | default: |
---|
| 1911 | continue; |
---|
| 1912 | } |
---|
[0] | 1913 | |
---|
[2730] | 1914 | if ( streams_get_flag(i, ROAR_FLAG_PAUSE) ) |
---|
| 1915 | continue; |
---|
| 1916 | |
---|
[0] | 1917 | if ( streams_get_outputbuffer(i, &bufs[have], ROAR_OUTPUT_CALC_OUTBUFSIZE(info)) == -1 ) { |
---|
| 1918 | ROAR_ERR("streams_get_mixbuffer(*): Can not alloc output buffer for stream %i, BAD!", i); |
---|
| 1919 | ROAR_ERR("streams_get_mixbuffer(*): Ignoring stream for this round."); |
---|
| 1920 | continue; |
---|
| 1921 | } |
---|
[2061] | 1922 | if ( streams_fill_mixbuffer2(i, info) == -1 ) { |
---|
[0] | 1923 | ROAR_ERR("streams_get_mixbuffer(*): Can not fill output buffer for stream %i, this should not happen", i); |
---|
| 1924 | continue; |
---|
| 1925 | } |
---|
| 1926 | |
---|
[139] | 1927 | // printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
| 1928 | |
---|
[0] | 1929 | ROAR_DBG("streams_get_mixbuffers(*): bufs[have] = %p", bufs[have]); |
---|
| 1930 | ROAR_DBG("streams_get_mixbuffers(*): *bufs[have] = 0x%08x...", *(uint32_t*)bufs[have]); |
---|
| 1931 | |
---|
[1887] | 1932 | if ( !streams_get_flag(i, ROAR_FLAG_MUTE) ) |
---|
| 1933 | have++; // we have a new stream! |
---|
[0] | 1934 | } |
---|
| 1935 | } |
---|
| 1936 | |
---|
| 1937 | bufs[have] = NULL; |
---|
[139] | 1938 | //printf("D: bufs[have=%i] = %p\n", have, bufs[have]); |
---|
[0] | 1939 | |
---|
| 1940 | ROAR_DBG("streams_get_mixbuffers(*): have = %i", have); |
---|
| 1941 | |
---|
| 1942 | *bufferlist = bufs; |
---|
[547] | 1943 | return have; |
---|
[0] | 1944 | } |
---|
| 1945 | |
---|
| 1946 | |
---|
| 1947 | int stream_add_buffer (int id, struct roar_buffer * buf) { |
---|
| 1948 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 1949 | |
---|
[2734] | 1950 | _CHECK_SID(id); |
---|
[0] | 1951 | |
---|
| 1952 | if ( g_streams[id]->buffer == NULL ) { |
---|
| 1953 | g_streams[id]->buffer = buf; |
---|
| 1954 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = 0", id, buf); |
---|
| 1955 | return 0; |
---|
| 1956 | } |
---|
| 1957 | |
---|
| 1958 | ROAR_DBG("stream_add_buffer(id=%i, buf=%p) = ?", id, buf); |
---|
| 1959 | return roar_buffer_add(g_streams[id]->buffer, buf); |
---|
| 1960 | } |
---|
| 1961 | |
---|
[2061] | 1962 | int stream_shift_out_buffer (int id, void * data, size_t * len) { |
---|
[2734] | 1963 | _CHECK_SID(id); |
---|
[2061] | 1964 | |
---|
| 1965 | if ( g_streams[id]->buffer == NULL ) |
---|
| 1966 | return -1; |
---|
| 1967 | |
---|
| 1968 | return roar_buffer_shift_out(&(g_streams[id]->buffer), data, len); |
---|
| 1969 | } |
---|
| 1970 | |
---|
[2151] | 1971 | int stream_outputbuffer_request(int id, struct roar_buffer ** buf, size_t len) { |
---|
| 1972 | register struct roar_stream_server * ss; |
---|
| 1973 | size_t buflen; |
---|
[2159] | 1974 | void * bufdata; |
---|
[2151] | 1975 | int ret; |
---|
| 1976 | |
---|
[2734] | 1977 | _CHECK_SID(id); |
---|
| 1978 | |
---|
[2151] | 1979 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 1980 | return -1; |
---|
| 1981 | |
---|
| 1982 | if ( buf != NULL ) /* just be be sure */ |
---|
| 1983 | *buf = NULL; |
---|
| 1984 | |
---|
| 1985 | if ( ss->outputbuffer != NULL ) { |
---|
| 1986 | if ( roar_buffer_get_len(ss->outputbuffer, &buflen) == 0 ) { |
---|
| 1987 | if ( buflen == len ) { |
---|
| 1988 | if ( buf != NULL ) |
---|
| 1989 | *buf = ss->outputbuffer; |
---|
| 1990 | return 0; |
---|
| 1991 | } else if ( buflen > len ) { |
---|
| 1992 | if ( roar_buffer_set_len(ss->outputbuffer, len) == 0 ) { |
---|
| 1993 | if ( buf != NULL ) |
---|
| 1994 | *buf = ss->outputbuffer; |
---|
| 1995 | return 0; |
---|
| 1996 | } |
---|
| 1997 | } |
---|
| 1998 | } |
---|
| 1999 | |
---|
| 2000 | // if the buffer is not suitable: |
---|
| 2001 | |
---|
| 2002 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 2003 | ss->outputbuffer = NULL; |
---|
| 2004 | if ( ret == -1 ) |
---|
| 2005 | return ret; |
---|
| 2006 | } |
---|
| 2007 | |
---|
| 2008 | if ( roar_buffer_new(&(ss->outputbuffer), len) == -1 ) |
---|
| 2009 | return -1; |
---|
| 2010 | |
---|
[2159] | 2011 | if ( roar_buffer_get_data(ss->outputbuffer, &bufdata) == -1 ) { |
---|
| 2012 | roar_buffer_free(ss->outputbuffer); |
---|
| 2013 | ss->outputbuffer = NULL; |
---|
| 2014 | return -1; |
---|
| 2015 | } |
---|
| 2016 | |
---|
| 2017 | memset(bufdata, 0, len); |
---|
| 2018 | |
---|
[2151] | 2019 | if ( buf != NULL ) |
---|
| 2020 | *buf = ss->outputbuffer; |
---|
| 2021 | |
---|
| 2022 | return 0; |
---|
| 2023 | } |
---|
| 2024 | |
---|
| 2025 | int stream_outputbuffer_destroy(int id) { |
---|
| 2026 | int ret; |
---|
| 2027 | register struct roar_stream_server * ss; |
---|
| 2028 | |
---|
[2734] | 2029 | _CHECK_SID(id); |
---|
| 2030 | |
---|
[2151] | 2031 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 2032 | return -1; |
---|
| 2033 | |
---|
| 2034 | if ( ss->outputbuffer != NULL ) { |
---|
| 2035 | ret = roar_buffer_free(ss->outputbuffer); |
---|
| 2036 | ss->outputbuffer = NULL; |
---|
| 2037 | return ret; |
---|
| 2038 | } |
---|
| 2039 | |
---|
| 2040 | return 0; |
---|
| 2041 | } |
---|
| 2042 | |
---|
[2816] | 2043 | int stream_prethru_add(int id, struct roar_buffer * buf) { |
---|
| 2044 | register struct roar_stream_server * ss; |
---|
| 2045 | |
---|
| 2046 | _CHECK_SID(id); |
---|
| 2047 | |
---|
| 2048 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 2049 | return -1; |
---|
| 2050 | |
---|
| 2051 | if ( ss->prethru == NULL ) { |
---|
| 2052 | ss->prethru = buf; |
---|
| 2053 | return 0; |
---|
| 2054 | } |
---|
| 2055 | |
---|
| 2056 | if ( roar_buffer_add(ss->prethru, buf) == -1 ) { |
---|
| 2057 | return -1; |
---|
| 2058 | } |
---|
| 2059 | |
---|
| 2060 | return 0; |
---|
| 2061 | } |
---|
| 2062 | |
---|
| 2063 | int stream_prethru_add_data(int id, void ** buf, size_t len) { |
---|
| 2064 | struct roar_buffer * buffer; |
---|
| 2065 | |
---|
| 2066 | _CHECK_SID(id); |
---|
| 2067 | |
---|
| 2068 | if ( roar_buffer_new(&buffer, len) == -1 ) |
---|
| 2069 | return -1; |
---|
| 2070 | |
---|
| 2071 | if ( roar_buffer_get_data(buffer, buf) == -1 ) { |
---|
| 2072 | roar_buffer_free(buffer); |
---|
| 2073 | return -1; |
---|
| 2074 | } |
---|
| 2075 | |
---|
| 2076 | if ( stream_prethru_add(id, buffer) == -1 ) { |
---|
| 2077 | roar_buffer_free(buffer); |
---|
| 2078 | return -1; |
---|
| 2079 | } |
---|
| 2080 | |
---|
| 2081 | return 0; |
---|
| 2082 | } |
---|
| 2083 | |
---|
| 2084 | int stream_prethru_destroy(int id) { |
---|
| 2085 | int ret; |
---|
| 2086 | register struct roar_stream_server * ss; |
---|
| 2087 | |
---|
| 2088 | _CHECK_SID(id); |
---|
| 2089 | |
---|
| 2090 | if ( (ss = g_streams[id]) == NULL ) |
---|
| 2091 | return -1; |
---|
| 2092 | |
---|
| 2093 | if ( ss->prethru != NULL ) { |
---|
| 2094 | ret = roar_buffer_free(ss->prethru); |
---|
| 2095 | ss->prethru = NULL; |
---|
| 2096 | return ret; |
---|
| 2097 | } |
---|
| 2098 | |
---|
| 2099 | return 0; |
---|
| 2100 | } |
---|
| 2101 | |
---|
| 2102 | int stream_prethru_send(int dst, int src) { |
---|
| 2103 | struct roar_stream_server * dst_ss, * src_ss; |
---|
| 2104 | struct roar_buffer * bufbuf; |
---|
| 2105 | void * bufdata; |
---|
| 2106 | size_t buflen; |
---|
| 2107 | |
---|
| 2108 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i) = ?", dst, src); |
---|
| 2109 | |
---|
| 2110 | _CHECK_SID(dst); |
---|
| 2111 | _CHECK_SID(src); |
---|
| 2112 | |
---|
| 2113 | if ( (dst_ss = g_streams[dst]) == NULL ) |
---|
| 2114 | return -1; |
---|
| 2115 | |
---|
| 2116 | if ( (src_ss = g_streams[src]) == NULL ) |
---|
| 2117 | return -1; |
---|
| 2118 | |
---|
| 2119 | bufbuf = src_ss->prethru; |
---|
| 2120 | |
---|
| 2121 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i): prethru buffer at %p", dst, src, bufbuf); |
---|
| 2122 | |
---|
| 2123 | while (bufbuf != NULL) { |
---|
| 2124 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i): looping with buffer at %p", dst, src, bufbuf); |
---|
| 2125 | |
---|
| 2126 | if ( roar_buffer_get_data(bufbuf, &bufdata) == -1 ) |
---|
| 2127 | return -1; |
---|
| 2128 | |
---|
| 2129 | if ( roar_buffer_get_len(bufbuf, &buflen) == -1 ) |
---|
| 2130 | return -1; |
---|
| 2131 | |
---|
| 2132 | if ( stream_vio_s_write(dst_ss, bufdata, buflen) != buflen ) |
---|
| 2133 | return -1; |
---|
| 2134 | |
---|
| 2135 | if ( roar_buffer_get_next(bufbuf, &bufbuf) == -1 ) |
---|
| 2136 | return -1; |
---|
| 2137 | } |
---|
| 2138 | |
---|
| 2139 | ROAR_DBG("stream_prethru_send(dst=%i, src=%i) = 0", dst, src); |
---|
| 2140 | return 0; |
---|
| 2141 | } |
---|
| 2142 | |
---|
[0] | 2143 | int streams_check (int id) { |
---|
| 2144 | int fh; |
---|
[508] | 2145 | ssize_t req, realreq, done; |
---|
[0] | 2146 | struct roar_stream * s; |
---|
| 2147 | struct roar_stream_server * ss; |
---|
| 2148 | struct roar_buffer * b; |
---|
[4542] | 2149 | void * buf; |
---|
[2248] | 2150 | // char tmp; |
---|
[0] | 2151 | |
---|
[2734] | 2152 | _CHECK_SID(id); |
---|
[0] | 2153 | |
---|
| 2154 | ROAR_DBG("streams_check(id=%i) = ?", id); |
---|
| 2155 | |
---|
[609] | 2156 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 2157 | |
---|
| 2158 | if ( (fh = s->fh) == -1 ) |
---|
| 2159 | return 0; |
---|
| 2160 | |
---|
[1821] | 2161 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
[0] | 2162 | return 0; |
---|
| 2163 | |
---|
[1821] | 2164 | switch (s->dir) { |
---|
| 2165 | case ROAR_DIR_LIGHT_IN: |
---|
[2493] | 2166 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1821] | 2167 | return light_check_stream(id); |
---|
[2493] | 2168 | #else |
---|
| 2169 | streams_delete(id); |
---|
| 2170 | return -1; |
---|
| 2171 | #endif |
---|
[1821] | 2172 | break; |
---|
[1845] | 2173 | case ROAR_DIR_MIDI_IN: |
---|
[2493] | 2174 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1845] | 2175 | return midi_check_stream(id); |
---|
[2493] | 2176 | #else |
---|
| 2177 | streams_delete(id); |
---|
| 2178 | return -1; |
---|
| 2179 | #endif |
---|
[1845] | 2180 | break; |
---|
[2237] | 2181 | case ROAR_DIR_RAW_IN: |
---|
[2493] | 2182 | #ifndef ROAR_WITHOUT_DCOMP_RAW |
---|
[2237] | 2183 | return raw_check_stream(id); |
---|
[2493] | 2184 | #else |
---|
| 2185 | streams_delete(id); |
---|
| 2186 | return -1; |
---|
| 2187 | #endif |
---|
[2237] | 2188 | break; |
---|
[2721] | 2189 | case ROAR_DIR_RDTCS_IN: |
---|
| 2190 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 2191 | return rdtcs_check_stream(id); |
---|
| 2192 | #else |
---|
| 2193 | streams_delete(id); |
---|
| 2194 | return -1; |
---|
| 2195 | #endif |
---|
| 2196 | break; |
---|
[1821] | 2197 | case ROAR_DIR_PLAY: |
---|
| 2198 | case ROAR_DIR_BIDIR: |
---|
| 2199 | break; |
---|
[4815] | 2200 | case ROAR_DIR_OUTPUT: |
---|
| 2201 | if ( !streams_get_flag(id, ROAR_FLAG_RECSOURCE) ) |
---|
| 2202 | return 0; |
---|
| 2203 | break; |
---|
[1821] | 2204 | default: |
---|
[2248] | 2205 | /* |
---|
| 2206 | ROAR_WARN("streams_check(id=%i): Read event on non input stream of type/dir %s", id, roar_dir2str(s->dir)); |
---|
| 2207 | errno = 0; |
---|
| 2208 | req = stream_vio_s_read(ss, &tmp, 1); |
---|
| 2209 | ROAR_DBG("streams_check(id=%i): stream_vio_s_read(ss, &tmp, 1) = %li // errno=%s(%i)", id, req, strerror(errno), errno); |
---|
| 2210 | if ( req == 1 ) { |
---|
| 2211 | ROAR_ERR("streams_check(id=%i): Client violates protocol, got one byte of data on output stream, kicking stream"); |
---|
| 2212 | streams_delete(id); |
---|
| 2213 | return -1; |
---|
| 2214 | } |
---|
| 2215 | */ |
---|
[1821] | 2216 | return 0; |
---|
| 2217 | break; |
---|
| 2218 | } |
---|
[1585] | 2219 | |
---|
[0] | 2220 | ROAR_DBG("streams_check(id=%i): fh = %i", id, fh); |
---|
| 2221 | |
---|
[2387] | 2222 | /* |
---|
| 2223 | 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); |
---|
| 2224 | */ |
---|
| 2225 | |
---|
| 2226 | req = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // optimal size |
---|
| 2227 | // req = ROAR_OUTPUT_BUFFER_SAMPLES * s->info.channels * (s->info.bits / 8) * ((float)s->info.rate/g_sa->rate); |
---|
[0] | 2228 | req += ss->need_extra; // bytes left we sould get.... |
---|
| 2229 | |
---|
[2387] | 2230 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
| 2231 | |
---|
[5242] | 2232 | if ( roar_buffer_new_data(&b, req, &buf) == -1 ) { |
---|
[0] | 2233 | ROAR_ERR("streams_check(*): Can not alloc buffer space!"); |
---|
| 2234 | ROAR_DBG("streams_check(*) = -1"); |
---|
| 2235 | return -1; |
---|
| 2236 | } |
---|
| 2237 | |
---|
| 2238 | ROAR_DBG("streams_check(id=%i): buffer is up and ready ;)", id); |
---|
[2387] | 2239 | ROAR_DBG("streams_check(id=%i): asking for %i bytes", id, req); |
---|
[0] | 2240 | |
---|
[270] | 2241 | if ( ss->codecfilter == -1 ) { |
---|
[508] | 2242 | realreq = req; |
---|
| 2243 | /* |
---|
[270] | 2244 | req = read(fh, buf, req); |
---|
[508] | 2245 | if ( req < realreq ) { // we can do this as the stream is in nonblocking mode! |
---|
| 2246 | if ( (realreq = read(fh, buf+req, realreq-req)) > 0 ) |
---|
| 2247 | req += realreq; |
---|
| 2248 | } |
---|
| 2249 | */ |
---|
| 2250 | done = 0; |
---|
| 2251 | while (req > 0 && done != realreq) { |
---|
[881] | 2252 | if ( (req = stream_vio_s_read(ss, buf+done, realreq-done)) > 0 ) |
---|
[508] | 2253 | done += req; |
---|
| 2254 | } |
---|
| 2255 | req = done; |
---|
[1837] | 2256 | |
---|
[270] | 2257 | } else { |
---|
| 2258 | req = codecfilter_read(ss->codecfilter_inst, ss->codecfilter, buf, req); |
---|
| 2259 | } |
---|
| 2260 | |
---|
| 2261 | if ( req > 0 ) { |
---|
[0] | 2262 | ROAR_DBG("streams_check(id=%i): got %i bytes", id, req); |
---|
| 2263 | |
---|
[5257] | 2264 | if ( roar_buffer_set_len(b, req) != -1 ) { |
---|
| 2265 | if ( stream_add_buffer(id, b) != -1 ) |
---|
| 2266 | return 0; |
---|
| 2267 | |
---|
| 2268 | ROAR_ERR("streams_check(id=%i): something is wrong, could not add buffer to stream!", id); |
---|
| 2269 | } else { |
---|
| 2270 | ROAR_ERR("streams_check(id=%i): something is wrong, could not set size of buffer!", id); |
---|
| 2271 | } |
---|
| 2272 | |
---|
[0] | 2273 | roar_buffer_free(b); |
---|
| 2274 | } else { |
---|
[272] | 2275 | ROAR_DBG("streams_check(id=%i): read() = %i // errno: %s", id, req, strerror(errno)); |
---|
| 2276 | #ifdef ROAR_HAVE_LIBVORBISFILE |
---|
| 2277 | if ( errno != EAGAIN && errno != ESPIPE ) { // libvorbis file trys to seek a bit ofen :) |
---|
| 2278 | #else |
---|
| 2279 | if ( errno != EAGAIN ) { |
---|
| 2280 | #endif |
---|
| 2281 | ROAR_DBG("streams_check(id=%i): EOF!", id); |
---|
| 2282 | streams_delete(id); |
---|
| 2283 | ROAR_DBG("streams_check(id=%i) = 0", id); |
---|
| 2284 | } |
---|
[334] | 2285 | roar_buffer_free(b); |
---|
[0] | 2286 | return 0; |
---|
| 2287 | } |
---|
| 2288 | |
---|
| 2289 | |
---|
| 2290 | ROAR_DBG("streams_check(id=%i) = -1", id); |
---|
| 2291 | return -1; |
---|
| 2292 | } |
---|
| 2293 | |
---|
| 2294 | |
---|
[2152] | 2295 | #define _return(x) return (x) |
---|
[0] | 2296 | int streams_send_mon (int id) { |
---|
[936] | 2297 | // int fh; |
---|
[0] | 2298 | struct roar_stream * s; |
---|
| 2299 | struct roar_stream_server * ss; |
---|
[2146] | 2300 | struct roar_buffer * bufbuf = NULL; |
---|
[2155] | 2301 | struct roar_remove_state removalstate; |
---|
[4815] | 2302 | void * ip = g_output_buffer; |
---|
[1157] | 2303 | void * obuf; |
---|
| 2304 | int olen; |
---|
[2150] | 2305 | int is_the_same = 1; |
---|
| 2306 | int is_vol_eq = 1; |
---|
[2155] | 2307 | int antiecho = 0; |
---|
[1157] | 2308 | ssize_t ret; |
---|
[0] | 2309 | |
---|
[2734] | 2310 | _CHECK_SID(id); |
---|
[0] | 2311 | |
---|
| 2312 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 2313 | |
---|
[585] | 2314 | s = ROAR_STREAM((ss = g_streams[id])); |
---|
[0] | 2315 | |
---|
[934] | 2316 | /* |
---|
[0] | 2317 | if ( (fh = s->fh) == -1 ) |
---|
| 2318 | return 0; |
---|
[934] | 2319 | */ |
---|
[0] | 2320 | |
---|
[1914] | 2321 | if ( !ss->ready ) |
---|
| 2322 | return 0; |
---|
[930] | 2323 | |
---|
[3042] | 2324 | if ( g_config->jumbo_mtu ) |
---|
| 2325 | roar_vio_sync(ss->viop); |
---|
| 2326 | |
---|
[1585] | 2327 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 2328 | return 0; |
---|
| 2329 | |
---|
[1821] | 2330 | switch (s->dir) { |
---|
| 2331 | case ROAR_DIR_LIGHT_OUT: |
---|
[2493] | 2332 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
[1821] | 2333 | return light_send_stream(id); |
---|
[2493] | 2334 | #else |
---|
| 2335 | streams_delete(id); |
---|
| 2336 | return -1; |
---|
| 2337 | #endif |
---|
[1821] | 2338 | break; |
---|
[1845] | 2339 | case ROAR_DIR_MIDI_OUT: |
---|
[2493] | 2340 | #ifndef ROAR_WITHOUT_DCOMP_MIDI |
---|
[1845] | 2341 | return midi_send_stream(id); |
---|
[2493] | 2342 | #else |
---|
| 2343 | streams_delete(id); |
---|
| 2344 | return -1; |
---|
| 2345 | #endif |
---|
[1845] | 2346 | break; |
---|
[2721] | 2347 | case ROAR_DIR_RDTCS_OUT: |
---|
| 2348 | #ifndef ROAR_WITHOUT_DCOMP_RDTCS |
---|
| 2349 | return rdtcs_send_stream(id); |
---|
| 2350 | #else |
---|
| 2351 | streams_delete(id); |
---|
| 2352 | return -1; |
---|
| 2353 | #endif |
---|
| 2354 | break; |
---|
[2694] | 2355 | |
---|
| 2356 | case ROAR_DIR_COMPLEX_OUT: |
---|
| 2357 | // send a tick: |
---|
| 2358 | if ( ss->codecfilter != -1 ) { |
---|
| 2359 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, NULL, 0) == 0 ) |
---|
[4237] | 2360 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
[4336] | 2361 | //ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
| 2362 | _streams_change_state(ss, ROAR_STREAMSTATE_OLD, "streams_send_mon"); |
---|
[4237] | 2363 | } |
---|
[2694] | 2364 | } |
---|
| 2365 | return 0; |
---|
| 2366 | break; |
---|
| 2367 | |
---|
[1821] | 2368 | case ROAR_DIR_OUTPUT: |
---|
| 2369 | if ( g_standby ) |
---|
| 2370 | return 0; |
---|
| 2371 | case ROAR_DIR_MONITOR: |
---|
| 2372 | case ROAR_DIR_BIDIR: |
---|
| 2373 | break; |
---|
| 2374 | |
---|
[4815] | 2375 | case ROAR_DIR_RECORD: |
---|
| 2376 | ip = g_input_buffer; |
---|
| 2377 | break; |
---|
[1821] | 2378 | default: |
---|
| 2379 | return 0; |
---|
| 2380 | break; |
---|
| 2381 | } |
---|
| 2382 | |
---|
| 2383 | |
---|
[1042] | 2384 | ROAR_DBG("streams_send_mon(id=%i): fh = %i", id, s->fh); |
---|
[0] | 2385 | |
---|
[625] | 2386 | if ( s->info.channels != g_sa->channels || s->info.bits != g_sa->bits || |
---|
[2147] | 2387 | s->info.rate != g_sa->rate || s->info.codec != g_sa->codec ) |
---|
| 2388 | is_the_same = 0; |
---|
| 2389 | |
---|
[2150] | 2390 | if ( !streams_get_flag(id, ROAR_FLAG_HWMIXER) ) { |
---|
| 2391 | is_vol_eq = need_vol_change(g_sa->channels, &(ss->mixer)) ? 0 : 1; |
---|
| 2392 | } |
---|
| 2393 | |
---|
[2155] | 2394 | if ( streams_get_flag(id, ROAR_FLAG_ANTIECHO) ) |
---|
| 2395 | antiecho = 1; |
---|
| 2396 | |
---|
| 2397 | if ( !is_the_same || !is_vol_eq || antiecho ) { |
---|
[625] | 2398 | olen = ROAR_OUTPUT_CALC_OUTBUFSIZE(&(s->info)); // we hope g_output_buffer_len |
---|
| 2399 | // is ROAR_OUTPUT_CALC_OUTBUFSIZE(g_sa) here |
---|
[2390] | 2400 | if ( stream_outputbuffer_request(id, &bufbuf, ROAR_OUTPUT_CALC_OUTBUFSIZE_MAX(&(s->info), g_sa)) == -1 ) |
---|
[625] | 2401 | return -1; |
---|
| 2402 | |
---|
[2146] | 2403 | if ( roar_buffer_get_data(bufbuf, &obuf) == -1 ) { |
---|
| 2404 | _return(-1); |
---|
| 2405 | } |
---|
| 2406 | |
---|
[625] | 2407 | ROAR_DBG("streams_send_mon(id=%i): obuf=%p, olen=%i", id, obuf, olen); |
---|
[2147] | 2408 | } else { |
---|
[4815] | 2409 | obuf = ip; |
---|
[2147] | 2410 | olen = g_output_buffer_len; |
---|
| 2411 | } |
---|
[625] | 2412 | |
---|
[2155] | 2413 | if ( antiecho ) { |
---|
[2397] | 2414 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2158] | 2415 | if ( roar_remove_init(&removalstate) == -1 ) { |
---|
[2155] | 2416 | _return(-1); |
---|
[2158] | 2417 | } |
---|
| 2418 | |
---|
[2397] | 2419 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2158] | 2420 | if ( roar_remove_so(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->bits, &removalstate) == -1 ) { |
---|
[2397] | 2421 | ROAR_DBG("streams_send_mon(id=%i): anti echo failed", id); |
---|
[2158] | 2422 | _return(-1); |
---|
| 2423 | } |
---|
[2397] | 2424 | ROAR_DBG("streams_send_mon(id=%i): antiecho=%i", id, antiecho); |
---|
[2155] | 2425 | } |
---|
| 2426 | |
---|
[2150] | 2427 | if ( !is_vol_eq ) { |
---|
[2938] | 2428 | if ( roar_amp_pcm(obuf, g_sa->bits, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa->channels, &(ss->mixer)) == -1 ) { |
---|
[2150] | 2429 | _return(-1); |
---|
| 2430 | } |
---|
| 2431 | |
---|
| 2432 | ip = obuf; |
---|
| 2433 | } |
---|
| 2434 | |
---|
[4288] | 2435 | streams_ltm_calc(id, &(s->info), ip, (ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels*g_sa->bits)/8); |
---|
| 2436 | |
---|
[2147] | 2437 | if ( !is_the_same ) { |
---|
[2148] | 2438 | if ( roar_conv(obuf, ip, ROAR_OUTPUT_BUFFER_SAMPLES*g_sa->channels, g_sa, &(s->info)) == -1 ) { |
---|
[2146] | 2439 | _return(-1); |
---|
[625] | 2440 | } |
---|
| 2441 | } |
---|
| 2442 | |
---|
[5256] | 2443 | roar_err_clear_errno(); |
---|
[585] | 2444 | |
---|
| 2445 | if ( ss->codecfilter == -1 ) { |
---|
[1042] | 2446 | ROAR_DBG("streams_send_mon(id=%i): not a CF stream", id); |
---|
[5256] | 2447 | if ( s->fh == -1 && ss->vio.inst == NULL ) { |
---|
[2705] | 2448 | ROAR_DBG("streams_send_mon(id=%i) = 0", id); |
---|
[2146] | 2449 | _return(0); |
---|
| 2450 | } |
---|
[1014] | 2451 | |
---|
[2705] | 2452 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 2453 | |
---|
[1157] | 2454 | if ( (ret = stream_vio_s_write(ss, obuf, olen)) == olen ) { |
---|
[981] | 2455 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[4237] | 2456 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
[4336] | 2457 | //ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
| 2458 | _streams_change_state(ss, ROAR_STREAMSTATE_OLD, "streams_send_mon"); |
---|
[4237] | 2459 | } |
---|
[2705] | 2460 | ROAR_DBG("streams_send_mon(id=%i) = 0", id); |
---|
[2146] | 2461 | _return(0); |
---|
[625] | 2462 | } |
---|
[1157] | 2463 | |
---|
[2705] | 2464 | ROAR_DBG("streams_send_mon(id=%i) = ?", id); |
---|
| 2465 | |
---|
[5256] | 2466 | if ( ret > 0 && roar_err_is_errno_clear() ) { |
---|
[1231] | 2467 | 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); |
---|
[4417] | 2468 | roar_notify_core_emit_simple(ROAR_OE_STREAM_XRUN, -1, id, ROAR_OT_STREAM, ROAR_XRUN_OVER_POST, -1, NULL, 0); |
---|
[1157] | 2469 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), ret)*s->info.channels); |
---|
[4237] | 2470 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
[4336] | 2471 | //ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
| 2472 | _streams_change_state(ss, ROAR_STREAMSTATE_OLD, "streams_send_mon"); |
---|
[4237] | 2473 | } |
---|
[2146] | 2474 | _return(0); |
---|
[1157] | 2475 | } |
---|
[585] | 2476 | } else { |
---|
[625] | 2477 | if ( codecfilter_write(ss->codecfilter_inst, ss->codecfilter, obuf, olen) |
---|
| 2478 | == olen ) { |
---|
[981] | 2479 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[4237] | 2480 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
[4336] | 2481 | //ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
| 2482 | _streams_change_state(ss, ROAR_STREAMSTATE_OLD, "streams_send_mon"); |
---|
[4237] | 2483 | } |
---|
[2146] | 2484 | _return(0); |
---|
[585] | 2485 | } else { // we cann't retry on codec filetered streams |
---|
[1012] | 2486 | if ( errno != EAGAIN ) { |
---|
| 2487 | streams_delete(id); |
---|
[2146] | 2488 | _return(-1); |
---|
[1012] | 2489 | } |
---|
[585] | 2490 | } |
---|
| 2491 | } |
---|
[0] | 2492 | |
---|
[129] | 2493 | if ( errno == EAGAIN ) { |
---|
| 2494 | // ok, the client blocks for a moment, we try to sleep a bit an retry in the hope not to |
---|
| 2495 | // make any gapes in any output because of this |
---|
| 2496 | |
---|
[1750] | 2497 | #ifdef ROAR_HAVE_USLEEP |
---|
[4896] | 2498 | roar_usleep(100); // 0.1ms |
---|
[1750] | 2499 | #endif |
---|
[129] | 2500 | |
---|
[881] | 2501 | if ( stream_vio_s_write(ss, obuf, olen) == olen ) { |
---|
[981] | 2502 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, ROAR_OUTPUT_CALC_OUTBUFSAMP(&(s->info), olen)*s->info.channels); |
---|
[4237] | 2503 | if ( ss->state != ROAR_STREAMSTATE_OLD ) { |
---|
[4336] | 2504 | //ROAR_INFO("streams_send_mon(id=%i): stream state: %s->old", ROAR_DBG_INFO_VERBOSE, id, roar_streamstate2str(ss->state)); |
---|
| 2505 | _streams_change_state(ss, ROAR_STREAMSTATE_OLD, "streams_send_mon"); |
---|
[4237] | 2506 | } |
---|
[2146] | 2507 | _return(0); |
---|
[1015] | 2508 | } else if ( errno == EAGAIN ) { |
---|
| 2509 | ROAR_WARN("streams_send_mon(id=%i): Can not send data to client: %s", id, strerror(errno)); |
---|
[2146] | 2510 | _return(0); |
---|
[625] | 2511 | } |
---|
[129] | 2512 | } |
---|
| 2513 | |
---|
[0] | 2514 | // ug... error... delete stream! |
---|
| 2515 | |
---|
| 2516 | streams_delete(id); |
---|
| 2517 | |
---|
[2146] | 2518 | _return(-1); |
---|
[0] | 2519 | } |
---|
[2146] | 2520 | #undef _return |
---|
[0] | 2521 | |
---|
[5022] | 2522 | int streams_send_filter_all(void) { |
---|
| 2523 | struct roar_stream_server * ss; |
---|
| 2524 | int i; |
---|
| 2525 | |
---|
| 2526 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 2527 | if ( (ss = g_streams[i]) == NULL ) |
---|
| 2528 | continue; |
---|
| 2529 | |
---|
| 2530 | if ( ROAR_STREAM(ss)->dir != ROAR_DIR_FILTER ) |
---|
| 2531 | continue; |
---|
| 2532 | |
---|
| 2533 | if ( !ss->ready ) |
---|
| 2534 | continue; |
---|
| 2535 | |
---|
| 2536 | if ( ss->state == ROAR_STREAMSTATE_CLOSING ) |
---|
| 2537 | continue; |
---|
| 2538 | |
---|
| 2539 | streams_send_filter(i); |
---|
| 2540 | } |
---|
| 2541 | |
---|
| 2542 | return 0; |
---|
| 2543 | } |
---|
| 2544 | |
---|
[0] | 2545 | int streams_send_filter(int id) { |
---|
| 2546 | int fh; |
---|
[127] | 2547 | int have = 0; |
---|
| 2548 | int len; |
---|
[0] | 2549 | struct roar_stream * s; |
---|
| 2550 | struct roar_stream_server * ss; |
---|
| 2551 | |
---|
[2734] | 2552 | _CHECK_SID(id); |
---|
[0] | 2553 | |
---|
| 2554 | ROAR_DBG("streams_send_filter(id=%i) = ?", id); |
---|
| 2555 | |
---|
[609] | 2556 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
[0] | 2557 | |
---|
| 2558 | if ( (fh = s->fh) == -1 ) |
---|
| 2559 | return 0; |
---|
| 2560 | |
---|
| 2561 | if ( s->dir != ROAR_DIR_FILTER ) |
---|
| 2562 | return 0; |
---|
| 2563 | |
---|
[1585] | 2564 | if ( streams_get_flag(id, ROAR_FLAG_PAUSE) ) |
---|
| 2565 | return 0; |
---|
| 2566 | |
---|
| 2567 | |
---|
[0] | 2568 | ROAR_DBG("streams_send_filter(id=%i): fh = %i", id, fh); |
---|
| 2569 | |
---|
[881] | 2570 | if ( stream_vio_s_write(ss, g_output_buffer, g_output_buffer_len) == g_output_buffer_len ) { |
---|
[127] | 2571 | while ( have < g_output_buffer_len ) { |
---|
[881] | 2572 | if ( (len = stream_vio_s_read(ss, g_output_buffer+have, g_output_buffer_len-have)) < 1 ) { |
---|
[127] | 2573 | streams_delete(id); |
---|
| 2574 | return -1; |
---|
| 2575 | } |
---|
| 2576 | have += len; |
---|
[0] | 2577 | } |
---|
[127] | 2578 | return 0; |
---|
[0] | 2579 | } |
---|
| 2580 | |
---|
| 2581 | // ug... error... delete stream! |
---|
| 2582 | |
---|
| 2583 | streams_delete(id); |
---|
| 2584 | |
---|
| 2585 | return -1; |
---|
| 2586 | } |
---|
| 2587 | |
---|
[596] | 2588 | |
---|
| 2589 | // VIO: |
---|
| 2590 | |
---|
| 2591 | ssize_t stream_vio_read (int stream, void *buf, size_t count) { |
---|
[2734] | 2592 | _CHECK_SID(stream); |
---|
[596] | 2593 | |
---|
[2734] | 2594 | return stream_vio_s_read(g_streams[stream], buf, count); |
---|
[596] | 2595 | } |
---|
| 2596 | |
---|
| 2597 | ssize_t stream_vio_write(int stream, void *buf, size_t count) { |
---|
[2734] | 2598 | _CHECK_SID(stream); |
---|
[596] | 2599 | |
---|
[2734] | 2600 | return stream_vio_s_write(g_streams[stream], buf, count); |
---|
[596] | 2601 | } |
---|
| 2602 | |
---|
[4815] | 2603 | int stream_vio_ctl (int stream, int cmd, void * data) { |
---|
| 2604 | _CHECK_SID(stream); |
---|
| 2605 | |
---|
| 2606 | return stream_vio_s_ctl(g_streams[stream], cmd, data); |
---|
| 2607 | } |
---|
[596] | 2608 | |
---|
| 2609 | ssize_t stream_vio_s_read (struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[2253] | 2610 | void * orig_buf = buf; |
---|
| 2611 | size_t len = 0; |
---|
| 2612 | ssize_t r = -1; |
---|
| 2613 | int i; |
---|
[739] | 2614 | |
---|
[596] | 2615 | errno = 0; |
---|
| 2616 | |
---|
[4987] | 2617 | if ( stream == NULL ) { |
---|
| 2618 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[596] | 2619 | return -1; |
---|
[4987] | 2620 | } |
---|
[596] | 2621 | |
---|
[1613] | 2622 | //roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[881] | 2623 | |
---|
[5265] | 2624 | if ( stream->vio.read == NULL ) { |
---|
[4987] | 2625 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
[596] | 2626 | return -1; |
---|
[4987] | 2627 | } |
---|
[596] | 2628 | |
---|
[881] | 2629 | while ( (r = roar_vio_read(&(stream->vio), buf, count)) > 0 ) { |
---|
[739] | 2630 | len += r; |
---|
| 2631 | buf += r; |
---|
| 2632 | count -= r; |
---|
| 2633 | if ( count == 0 ) |
---|
| 2634 | break; |
---|
| 2635 | } |
---|
| 2636 | |
---|
[740] | 2637 | if ( len == 0 && r == -1 ) |
---|
| 2638 | return -1; |
---|
| 2639 | |
---|
[2816] | 2640 | if ( streams_thru_num ) { |
---|
| 2641 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 2642 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) { |
---|
| 2643 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 2644 | if ( g_streams[i]->ready ) { |
---|
[2258] | 2645 | if ( stream_vio_write(i, orig_buf, len) != len ) |
---|
| 2646 | streams_delete(i); |
---|
[2252] | 2647 | |
---|
[4237] | 2648 | if ( g_streams[i] != NULL ) { |
---|
| 2649 | if ( g_streams[i]->state != ROAR_STREAMSTATE_OLD ) { |
---|
[4336] | 2650 | //ROAR_INFO("stream_vio_s_read(*): (stream: %i) stream state: %s->old", ROAR_DBG_INFO_VERBOSE, i, roar_streamstate2str(g_streams[i]->state)); |
---|
| 2651 | _streams_change_state(g_streams[i], ROAR_STREAMSTATE_OLD, "stream_vio_s_read"); |
---|
[4237] | 2652 | } |
---|
| 2653 | } |
---|
[2816] | 2654 | } |
---|
| 2655 | } |
---|
| 2656 | } |
---|
| 2657 | } |
---|
| 2658 | } |
---|
| 2659 | |
---|
[739] | 2660 | return len; |
---|
[596] | 2661 | } |
---|
| 2662 | |
---|
| 2663 | ssize_t stream_vio_s_write(struct roar_stream_server * stream, void *buf, size_t count) { |
---|
[2259] | 2664 | int i; |
---|
| 2665 | |
---|
[596] | 2666 | errno = 0; |
---|
| 2667 | |
---|
[4987] | 2668 | if ( stream == NULL ) { |
---|
| 2669 | roar_err_set(ROAR_ERROR_FAULT); |
---|
[596] | 2670 | return -1; |
---|
[4987] | 2671 | } |
---|
[596] | 2672 | |
---|
[1613] | 2673 | /* |
---|
[934] | 2674 | if ( roar_vio_get_fh(&(stream->vio)) == -1 && ROAR_STREAM(stream)->fh != -1 ) |
---|
| 2675 | roar_vio_set_fh(&(stream->vio), ROAR_STREAM(stream)->fh); |
---|
[1613] | 2676 | */ |
---|
[934] | 2677 | |
---|
| 2678 | // ROAR_WARN("stream_vio_s_write(*): writing..."); |
---|
[596] | 2679 | |
---|
[2816] | 2680 | if ( streams_thru_num ) { |
---|
| 2681 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 2682 | if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == ROAR_STREAM(stream)->id ) { |
---|
| 2683 | if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) { |
---|
| 2684 | if ( g_streams[i]->ready ) { |
---|
| 2685 | if ( g_streams[i]->state == ROAR_STREAMSTATE_NEW ) { |
---|
| 2686 | if ( streams_get_flag(i, ROAR_FLAG_PRETHRU) == 1 ) { |
---|
[3042] | 2687 | if ( stream_prethru_send(i, ROAR_STREAM(stream)->id) == -1 ) { |
---|
[2816] | 2688 | streams_delete(i); |
---|
| 2689 | } |
---|
| 2690 | } |
---|
| 2691 | } |
---|
| 2692 | |
---|
| 2693 | if ( stream_vio_write(i, buf, count) != count ) { |
---|
[2259] | 2694 | streams_delete(i); |
---|
[2816] | 2695 | } |
---|
| 2696 | |
---|
[4237] | 2697 | if ( g_streams[i] != NULL ) { |
---|
| 2698 | if ( g_streams[i]->state != ROAR_STREAMSTATE_OLD ) { |
---|
[4336] | 2699 | //ROAR_INFO("stream_vio_s_write(*): (stream: %i) stream state: %s->old", ROAR_DBG_INFO_VERBOSE, i, roar_streamstate2str(g_streams[i]->state)); |
---|
| 2700 | _streams_change_state(g_streams[i], ROAR_STREAMSTATE_OLD, "stream_vio_s_write"); |
---|
[4237] | 2701 | } |
---|
| 2702 | } |
---|
[2816] | 2703 | } |
---|
| 2704 | } |
---|
| 2705 | } |
---|
| 2706 | } |
---|
| 2707 | } |
---|
[2259] | 2708 | |
---|
[3042] | 2709 | if ( g_config->jumbo_mtu ) { |
---|
| 2710 | if ( stream->viop != &(stream->jumbo) ) { |
---|
| 2711 | if ( roar_vio_open_jumbo(&(stream->jumbo), &(stream->vio), g_config->jumbo_mtu) != -1 ) { |
---|
| 2712 | // if that works we continue using the jumbo vio, |
---|
| 2713 | // in case it didn't we dont, just use normal VIO. |
---|
| 2714 | stream->viop = &(stream->jumbo); |
---|
| 2715 | } |
---|
| 2716 | } |
---|
| 2717 | } |
---|
| 2718 | |
---|
| 2719 | return roar_vio_write(stream->viop, buf, count); |
---|
[596] | 2720 | } |
---|
| 2721 | |
---|
[4815] | 2722 | int stream_vio_s_ctl (struct roar_stream_server * stream, int cmd, void * data) { |
---|
| 2723 | if ( stream == NULL ) |
---|
| 2724 | return -1; |
---|
| 2725 | |
---|
| 2726 | return roar_vio_ctl(stream->viop, cmd, data); |
---|
| 2727 | } |
---|
| 2728 | |
---|
[0] | 2729 | //ll |
---|