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