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