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