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